您的位置:首页 > 其它

【2012-12】【斐波那契循环节】Evil Teacher

2015-01-11 20:55 323 查看
定理:

设P(m)为斐波那契模m的循环节

若m=p1^t1*p2^t2...

则P(m)=lcm(P(p1^t1),P(p2^t2),...)

又有定理:

P(p^t)<=2(p+1)p^t (p>5)

更有定理:

P(m)<=6m

于是可以暴力

还有个显然的结论:

P(p^t)=P(p)*p^(t-1)

哈哈~讲课讲到这了

//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive    Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=(l),___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=(r),___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const int read()
{int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
int m;
LL ans;
/////////////////////////////////////////////////
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
int P(int m)
{
int x=1,y=1,z,t=1;
while(1)
{
t++;
z=(x+y)%m;
x=y; y=z;
if(x==0&&y==1) return t;
}
return 0;
}
/////////////////////////////////////////////////
void input()
{
m=read();
}
void solve()
{
ans=1;
for(int i=2;i*i<=m;i==2?i++:i+=2) if(m%i==0)
{
LL t=P(i); m/=i;
while(m%i==0) m/=i,t*=i;
ans=lcm(ans,t);
}
if(m>1) ans=lcm(ans,P(m));
cout<<ans<<endl;
}
/////////////////////////////////////////////////
int main()
{
#ifndef _TEST
freopen("std.in","r",stdin); freopen("std.out","w",stdout);
#endif
rep(i,1,read())
input(),solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: