标签:
题目来源17748018
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32)if(c==EOF)return 0;
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return 1;
}
template<class T> inline T read_(T&x,T&y)
{
return read(x)&&read(y);
}
template<class T> inline T read__(T&x,T&y,T&z)
{
return read(x)&&read(y)&&read(z);
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//-------ZCC IO template------
const int maxn=1000001;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 1000000007
typedef struct Matrix
{
LL m[3][3];
}mat;
mat mul(mat a,mat b)
{
mat c;
memset(c.m,0,sizeof(c.m));
for(int i=0;i<1;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
c.m[i][j]+=((a.m[i][k]*b.m[k][j])%mod+mod)%mod;
return c;
}
mat mu(mat a,mat b)
{
mat c;
memset(c.m,0,sizeof(c.m));
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
c.m[i][j]+=((a.m[i][k]*b.m[k][j])%mod+mod)%mod;
return c;
}
mat matpow(mat a,int n)
{
mat rec;
memset(rec.m,0,sizeof(rec.m));
rec.m[0][0]=rec.m[1][1]=rec.m[2][2]=1;
while(n)
{
if(n&1)
rec=mu(rec,a);
a=mu(a,a);
n>>=1;
}
return rec;
}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
//freopen("zccccc.txt","w",stdout);
//#endif // ONLINE_JUDGE
int n,m,i,j,t,k;
while(read(n))
{
mat a,b;
a.m[0][0]=0;
a.m[0][1]=1;
b.m[0][0]=0;
b.m[0][1]=b.m[1][0]=b.m[1][1]=1;
//mat c=mul(a,b);
//printf("%d %d \n%d %d\n",c.m[0][0],c.m[0][1],c.m[1][0],c.m[1][1]);
writeln(mul(a,matpow(b,n-1)).m[0][1]%mod);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/45074963