思路:矩阵快速幂, 二分加速
#include<cstdio> #include<cstring> #define ll long long #define mod 10000 struct jz { ll num[2][2]; jz(){ memset(num, 0, sizeof(num)); } jz operator*(const jz&p)const { jz ans; for (int k = 0; k < 2;++k) for (int i = 0; i < 2;++i) for (int j = 0; j < 2; ++j) ans.num[i][j] = (ans.num[i][j] + num[i][k] * p.num[k][j] % mod) % mod; return ans; } }; jz POW(jz x, ll n) { jz ans; for (int i = 0; i < 2; ++i)ans.num[i][i] = 1; for (; n; n>>=1, x=x*x) if (n & 1){ ans = ans*x; } return ans; } int main() { ll n; while (scanf("%lld", &n) && (n != -1)) { if (n <= 1)printf("%lld\n", n); else { jz pp; pp.num[0][0] = 1; pp.num[0][1] = 1; pp.num[1][0] = 1; pp = POW(pp, n - 2); printf("%lld\n", (pp.num[0][0]+pp.num[0][1])%mod); } } }