循环节是2000000016
字符串读入,用一下高精度对2000000016取个模,用一下快速幂就可以算出答案了。
#include#include #include using namespace std;const long long MOD = 1e9+7;long long mod1(char *a1,int b){ long long a[5000] = { 0}; long long c[5000] = { 0}; long long i, k, d; k = strlen(a1); for(i = 0; i < k; i++) a[i] = a1[k - i - 1] - '0'; d = 0; for(i = k - 1; i >= 0 ; i--) { d = d * 10 + a[i]; c[i] = d / b; d = d % b; } while(c[k - 1] == 0 && k > 1) k--; return d;}struct matrix{ long long m[2][2];}ans, base; matrix multi(matrix a, matrix b){ matrix tmp; for(int i = 0; i < 2; ++i) { for(int j = 0; j < 2; ++j) { tmp.m[i][j] = 0; for(int k = 0; k < 2; ++k) tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]% MOD) % MOD; } } return tmp;}int fast_mod(int n) // 求矩阵 base 的 n 次幂{ base.m[0][0] = base.m[0][1] = base.m[1][0] = 1; base.m[1][1] = 0; ans.m[0][0] = ans.m[1][1] = 1; // ans 初始化为单位矩阵 ans.m[0][1] = ans.m[1][0] = 0; while(n) { if(n & 1) { ans = multi(ans, base); } base = multi(base, base); n >>= 1; } return ans.m[0][1];}char SS[1000];int main(){ int T; scanf("%d",&T); while(T--) { scanf("%s",SS); //printf("%d\n",mod1(SS,2000000016)); printf("%lld\n", fast_mod(mod1(SS,2000000016))%MOD); } return 0;}