hdu 4291 矩阵幂 循环节
生活随笔
收集整理的這篇文章主要介紹了
hdu 4291 矩阵幂 循环节
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://acm.hdu.edu.cn/showproblem.php?pid=4291
凡是取模的都有循環節-----常數有,矩陣也有,并且矩陣的更奇妙:
g(g(g(n))) mod 109?+ 7 ?最外層MOD=1e9+7 ?能夠算出g(g(n))的循環節222222224。進而算出g(n)的循環節183120LL。然后由內而外計算就可以
凝視掉的是求循環節的代碼
//#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <iostream> #include <iomanip> #include <cmath> #include <map> #include <set> #include <queue> using namespace std;#define ls(rt) rt*2 #define rs(rt) rt*2+1 #define ll long long #define ull unsigned long long #define rep(i,s,e) for(int i=s;i<e;i++) #define repe(i,s,e) for(int i=s;i<=e;i++) #define CL(a,b) memset(a,b,sizeof(a)) #define IN(s) freopen(s,"r",stdin) #define OUT(s) freopen(s,"w",stdout) const ll ll_INF = ((ull)(-1))>>1; const double EPS = 1e-8; const double pi = acos(-1.0); const int INF = 100000000; const ll MOD[3] = {183120LL,222222224LL,1000000007LL}; const int N = 2;struct Matrix{ll m[N][N];//int sz;//矩陣的大小 };Matrix I= {3LL,1LL,//要冪乘的矩陣1LL,0LL,}; Matrix unin={1LL,0LL,//單位矩陣0LL,1LL,}; Matrix matrixmul(Matrix a,Matrix b,long long mod)//矩陣a乘矩陣b {Matrix c;for(int i=0; i<N; i++)for(int j=0; j<N; j++){c.m[i][j]=0LL;for(int k=0; k<N; k++)c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;c.m[i][j]%=mod;}return c; } Matrix quickpow(long long n,long long mod) {Matrix m=I,b=unin;//求矩陣I的n階矩陣while(n>=1){if(n&1)b=matrixmul(b,m,mod);n=n>>1;m=matrixmul(m,m,mod);}return b; }ll solve(ll n) {ll ans;Matrix ret;ret.m[0][0]=n;for(int i=0;i<3;i++){if(ret.m[0][0]!=0 && ret.m[0][0]!=1)ret=quickpow(ret.m[0][0]-1,MOD[i]);}return ret.m[0][0]; }int main() {//precal();ll n;while(~scanf("%I64d",&n)){if(n==0){puts("0");continue;}if(n==1){puts("1");continue;}//printf("%I64d\n",solve(n));cout << solve(n)%1000000007LL << endl;}return 0; }
hdu 4291 矩陣冪 ?循環節
總結
以上是生活随笔為你收集整理的hdu 4291 矩阵幂 循环节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeSmith 基础用法和例子
- 下一篇: [转]SSH反向连接及Autossh