隨意選了一題,就作為部落格生涯的開端吧^^
#include<stdio.h>typedef __int64 LL;/*g(n) % MOD1 = g(n % MOD2) % MOD1;//MOD2為迴圈節,以下如上g(n) % MOD2 = g(n % MOD3) % MOD2; g(g(g(n))) % MOD1= g(g(g(n)) % MOD2) % MOD1= g(g(g(n) % MOD3) % MOD2) % MOD1;n = g(n) % MOD3;n = g(n) % MOD2;n = g(n) % MOD1;*/const LL MOD1 = 1000000007;const LL MOD2 = 222222224;const LL MOD3 = 183120;const LL base[2][2] = {3, 1, 1, 0};LL ans[2][2], tmp[2][2];/*int main()//求迴圈節{ LL n0 = 0, n1 = 1, tt; LL mod = MOD2;//改變該值 for(int i = 1; ;i++) { tt = (3 * n1 + n0) % mod; n0 = n1; n1 = tt; if(n0 == 0 && n1 == 1) { printf("%d\n", i); break; } }}*/inline void Mult(LL pro[][2], LL mul1[][2], LL mul2[][2], LL MOD)//ans = mul1 * mul2;{ LL m1[2][2], m2[2][2]; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) m1[i][j] = mul1[i][j], m2[i][j] = mul2[i][j]; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) { pro[i][j] = 0; for(int k = 0; k < 2; k++) pro[i][j] += m1[i][k] * m2[k][j]; pro[i][j] %= MOD; }}inline void FastPow(LL n, LL MOD)//矩陣的base^n{ ans[0][0] = 1, ans[0][1] = 0; ans[1][0] = 0, ans[1][1] = 1;//單位矩陣 for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) tmp[i][j] = base[i][j]; while(n) { if(n & 1) { Mult(ans, ans, tmp, MOD); } Mult(tmp, tmp, tmp, MOD); n >>= 1; }}int main(){ LL m; while(scanf("%I64d", &m) != EOF) { FastPow(m, MOD3); m = ans[1][0]; FastPow(m, MOD2); m = ans[1][0]; FastPow(m, MOD1); m = ans[1][0]; printf("%I64d\n", m); } return 0;}