求助0分

P1306 斐波那契公约数

取模
by lzyqwq @ 2022-08-03 09:59:20


这个代码AC了 ``` #include<iostream> #include<algorithm> #include<cstdio> #define MOD 100000000 using namespace std; int n,m,k; struct node{ long long a[3][3]; }ans,base,s; inline int read(){ int date=0,w=1;char c=0; while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();} while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();} return date*w; } int gcd(int x,int y){ if(y==0)return x; return gcd(y,x%y); } node operator *(const node &x,const node &y){ node ret; for(int i=1;i<=2;i++) for(int j=1;j<=2;j++){ ret.a[i][j]=0; for(int k=1;k<=2;k++){ ret.a[i][j]+=x.a[i][k]*y.a[k][j]; ret.a[i][j]%=MOD; } } return ret; } void mexp(int k){ for(int i=1;i<=2;i++)s.a[i][i]=1; base.a[1][2]=base.a[2][1]=base.a[2][2]=1; while(k){ if(k&1)s=s*base; base=base*base; k>>=1; } } int main(){ n=read();m=read(); k=gcd(n,m); if(k==1||k==2){ printf("1\n"); return 0; } mexp(k-2); ans.a[1][1]=ans.a[1][2]=1; ans=ans*s; printf("%lld\n",ans.a[1][2]); return 0; }
by WANG_ZHENG_MING @ 2022-08-03 10:25:14


|