HDu4794 斐波那契循环节

题意:Arnold变换把矩阵(x,y)变成((x+y)%n,(x+2*y)%n),问最小循环节

题解:仔细算前几项能看出是斐波那契数论modn,然后套个斐波那契循环节板子即可

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll unsigned long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=300000+10,inf=0x3f3f3f3f; struct Node{
ll row,col;
ll a[3][3];
};
Node mul(Node x,Node y,ll c)
{
Node ans;
ans.row=x.row,ans.col=y.col;
memset(ans.a,0,sizeof ans.a);
for(int i=0;i<x.row;i++)
for(int j=0;j<x.col;j++)
for(int k=0;k<y.col;k++)
ans.a[i][k]=(ans.a[i][k]+x.a[i][j]*y.a[j][k]+c)%c;
return ans;
}
Node quick_mul(Node x,ll n,ll c)
{
Node ans;
ans.row=x.row,ans.col=x.col;
memset(ans.a,0,sizeof ans.a);
for(int i=0;i<ans.col;i++)ans.a[i][i]=1;
while(n){
if(n&1)ans=mul(ans,x,c);
x=mul(x,x,c);
n>>=1;
}
return ans;
}
bool ok(ll x,ll n)
{
Node A;A.row=A.col=2;
A.a[0][0]=A.a[0][1]=A.a[1][0]=1;A.a[1][1]=0;
A=quick_mul(A,x-1,n);
return ((A.a[0][0]+A.a[0][1])%n==1)&&((A.a[1][0]+A.a[1][1])%n==0);
}
bool erci(ll x,ll p){return qp(x,(p-1)>>1,p)==1;}
ll n,fac[N],cnt;
vector<pli>v;
ll fibmod(ll n)
{
v.clear();
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
int co=0;
while(n%i==0)n/=i,co++;
v.pb(mp(i,co));
}
}
if(n!=1)v.pb(mp(n,1));
ll ans=1;
for(int i=0;i<v.size();i++)
{
ll p=v[i].fi,m=v[i].se;
ll te,gp,x;
if(p==2)gp=3;
else if(p==3)gp=8;
else if(p==5)gp=20;
else
{
if(erci(5,p))te=p-1;
else te=2*(p+1);
cnt=0;
for(ll j=1;j*j<=te;j++)
{
if(te%j==0)
{
fac[++cnt]=j;
if(j*j!=te)fac[++cnt]=te/j;
}
}
sort(fac+1,fac+1+cnt);
for(int j=1;j<=cnt;j++)
{
if(ok(fac[j],p))
{
gp=fac[j];
break;
}
}
}
x=gp*(ll)pow(p,m-1);
ans=ans/gcd(ans,x)*x;
}
return ans;
}
int main()
{
while(~scanf("%llu",&n))
{
if(n==2)puts("3");
else printf("%llu\n",fibmod(n)>>1ll);
}
return 0;
}
/******************** ********************/
上一篇:JOISC 2017 Day1 T3 烟花棒


下一篇:注册表缺失导致Windows Server 2008 R2时钟服务W32time不能自启