有个定理叫gcd(f(n),f(m))=f(gcd(n,m))
所以递推就好了。
#include<cstdio> #include<cstdlib> #include<cctype> #include<cstring> #include<algorithm> inline long long read(){ long long num=0,f=1; char ch=getchar(); while(!isdigit(ch)){ if(ch=='-') f=-1; ch=getchar(); } while(isdigit(ch)){ num=num*10+ch-'0'; ch=getchar(); } return num*f; } long long gcd(long long x,long long y){ return !y?x:gcd(y,x%y); } long long f[1001010]; int main(){ long long n=read(),m=read(); long long x=gcd(n,m); f[1]=f[2]=1; for(long long i=3;i<=x;++i) f[i]=(f[i-1]+f[i-2])%10000000; printf("%lld",f[x]); return 0; }