NOI2011 兔农

http://www.lydsy.com/JudgeOnline/problem.php?id=2432

感觉是day1中最难的一题,还好出题人很良心,给了75分部分分。

还是跪拜策爷吧~Orz

http://jcvb.is-programmer.com/posts/39528.html

代码奇丑。。。。。。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int sgn(DB x){if(abs(x)<EPS)return ;return(x>)?:-;}
const DB Pi=acos(-1.0); inline int gint()
{
int res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
}
inline LL gll()
{
LL res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
} const LL maxK=; LL N,K,P;
LL fib[*maxK+];
LL pos[maxK+];
LL len[maxK+],next[maxK+]; inline LL gcd(LL a,LL b){return b== ? a : gcd(b,a%b); }
inline void extend_gcd(LL a,LL &x,LL b,LL &y)
{
if(b==){x=;y=;return;}
LL dx,dy;
extend_gcd(b,dx,a%b,dy);
x=dy;
y=dx-a/b*dy;
} struct Tmatrix
{
int n,m;
LL v[][];
inline void clear(){n=m=;mmst(v,);}
inline friend Tmatrix operator *(Tmatrix a,Tmatrix b)
{
int i,j,k;
Tmatrix c;c.clear();
c.n=a.n;c.m=b.m;
re(i,,c.n)re(j,,c.m)re(k,,a.m)c.v[i][j]=(c.v[i][j]+a.v[i][k]*b.v[k][j]%P)%P;
return c;
}
}; Tmatrix A,B; inline Tmatrix power(Tmatrix a,LL k)
{
int i;
Tmatrix x,y=a;
x.clear();x.n=x.m=a.n;re(i,,a.n)x.v[i][i]=;
for(;k!=;k>>=){if(k&)x=x*y;y=y*y;}
return x;
} int flag[maxK+]; int main()
{
/*freopen("rabbit.in","r",stdin);
freopen("rabbit.out","w",stdout);*/
LL i;
N=gll();K=gll();P=gll();
fib[]=fib[]=;
for(i=;;i++)
{
fib[i]=(fib[i-]+fib[i-])%K;
if(!pos[fib[i]])pos[fib[i]]=i;
if(fib[i]== && fib[i-]==)break;
}
re(i,,K-)
{
LL x,y;
if(gcd(i,K)==)
{
extend_gcd(i,x,K,y);
x=(x%K+K)%K;
if(pos[x]==)
{
len[i]=-;
next[i]=-;
}
else
{
len[i]=pos[x];
next[i]=i*fib[len[i]-]%K;
}
}
else
len[i]=-,next[i]=-;
}
A.clear();
A.n=A.m=;
A.v[][]=;A.v[][]=;A.v[][]=;
A.v[][]=;A.v[][]=;A.v[][]=;
A.v[][]=;A.v[][]=;A.v[][]=;
B.clear();
B.n=B.m=;
B.v[][]=;B.v[][]=;B.v[][]=-;
B.v[][]=;B.v[][]=;B.v[][]=;
B.v[][]=;B.v[][]=;B.v[][]=; int p,t;
for(p=;!flag[p] && next[p]!=-;flag[p]=,p=next[p]);
if(next[p]!=-)
{
LL lenX=,lenY=;Tmatrix X,Y,Z;
X.clear();X.n=X.m=;X.v[][]=X.v[][]=X.v[][]=;
for(t=;t!=p;t=next[t])X=B*power(A,len[t])*X,lenX+=len[t];
if(N>=lenX)
{
Y.clear();Y.n=Y.m=;Y.v[][]=Y.v[][]=Y.v[][]=;
for(t=p,Y=B*power(A,len[t])*Y,lenY+=len[t],t=next[t];t!=p;t=next[t])Y=B*power(A,len[t])*Y,lenY+=len[t];
Z=power(Y,(N-lenX)/lenY)*X;
N=(N-lenX)%lenY;
for(t=p;;t=next[t])
if(N>=len[t])
{
Z=B*power(A,len[t])*Z;
N-=len[t];
}
else break;
LL y=(Z.v[][]+Z.v[][])%P,x=(Z.v[][]+Z.v[][])%P;
Y.clear();
Y.n=Y.m=;
Y.v[][]=;Y.v[][]=;
Y.v[][]=;Y.v[][]=;
Z=power(Y,N);
LL res=(Z.v[][]*y%P+Z.v[][]*x%P)%P;
cout<<(res%P+P)%P<<endl;
}
else
{
X.clear();X.n=X.m=;X.v[][]=X.v[][]=X.v[][]=;
for(t=;t!=p;t=next[t])
if(N>=len[t])
{
X=B*power(A,len[t])*X;
N-=len[t];
}
else break;
LL y=(X.v[][]+X.v[][])%P,x=(X.v[][]+X.v[][])%P;
Y.clear();
Y.n=Y.m=;
Y.v[][]=;Y.v[][]=;
Y.v[][]=;Y.v[][]=;
Z=power(Y,N);
LL res=(Z.v[][]*y%P+Z.v[][]*x%P)%P;
cout<<(res%P+P)%P<<endl;
}
}
else
{
LL lenX=;Tmatrix X,Y,Z;
X.clear();X.n=X.m=;X.v[][]=X.v[][]=X.v[][]=;
for(t=;t!=p;t=next[t])X=B*power(A,len[t])*X,lenX+=len[t];
if(N>=lenX)
{
LL y=(X.v[][]+X.v[][])%P,x=(X.v[][]+X.v[][])%P;
N-=lenX;
Y.clear();
Y.n=Y.m=;
Y.v[][]=;Y.v[][]=;
Y.v[][]=;Y.v[][]=;
Z=power(Y,N);
LL res=(Z.v[][]*y%P+Z.v[][]*x%P)%P;
cout<<(res%P+P)%P<<endl;
}
else
{
X.clear();X.n=X.m=;X.v[][]=X.v[][]=X.v[][]=;
for(t=;t!=p;t=next[t])
if(N>=len[t])
{
X=B*power(A,len[t])*X;
N-=len[t];
}
else break;
LL y=(X.v[][]+X.v[][])%P,x=(X.v[][]+X.v[][])%P;
Y.n=Y.m=;
Y.v[][]=;Y.v[][]=;
Y.v[][]=;Y.v[][]=;
Z=power(Y,N);
LL res=(Z.v[][]*y%P+Z.v[][]*x%P)%P;
cout<<(res%P+P)%P<<endl;
}
}
return ;
}
上一篇:我的操作系统复习——I/O控制和系统调用


下一篇:深入小程序系列之二、Flutter 和小程序混编