Subject: ask
Idea: This question gave me a new understanding of the recurring section QAQ: Let's first consider the recurring section of g(n) to 1e9+7, which is really not easy to find... You can guess that this recurring section should be at 1e7, The range of 1e8 can be divided into several ranges, and then slowly find this loop section
ll a=0,b=1,c; for(ll i=2;i<=300000000;i++) { c=(3*b+a)%mod1; a=b%mod1; b=c%mod1; if(a==0&&b==1) printf("%lld\n",i-1); }This will find the first loop section: 222222224
Then we consider the loop section of g(g(n)), because g(n) has a loop in 0-222222223, so it was originally modulo 1e9+7, which is actually equivalent to modulo 222222224
This finds the second loop section: 183120
Similarly, the loop section of g(g(g(n))) can be found: 240
It is easy to find the loop section calculation QAQ
#include<iostream> #include<algorithm> #include<cstdlib> #include<cstdio> #include<cstring> using namespace std; const int N=2; typedef long long ll; const int mod1=1e9+7; const int mod2=222222224; const int mod3=183120; const int mod4=240; struct matrix { ll m[N][N]; }; matrix matrixmul(matrix a,matrix b,int mod) { ll i,j,k; matrix c; for(i=0;i<N;i++) { for(j=0;j<N;j++) { c.m[i][j]=0; for(k=0;k<N;k++) { c.m[i][j]+=a.m[i][k]*b.m[k][j]%mod; } c.m[i][j]=c.m[i][j]%mod; } } return c; } matrix quickpow(ll n,int mod) { matrix m={3,1,1,0}; matrix c={1,0,0,1}; while(n>=1) { if(n&1) { c=matrixmul(c,m,mod); } n=n>>1; m=matrixmul(m,m,mod); } return c; } intmain() { ll n; while(scanf("%lld",&n)!=-1) { n=n%mod4; if(n==0) { printf("0\n"); continue; } matrix res1=quickpow(n-1,mod3); ll temp1=res1.m[0][0]; if(temp1==0) { printf("0\n"); continue; } matrix res2=quickpow(temp1-1,mod2); ll temp2=res2.m[0][0]; if(temp2==0) { printf("0\n"); continue; } matrix res3=quickpow(temp2-1,mod1); ll ans=res3.m[0][0]; printf("%lld\n",ans); } return 0; }