您的位置:首页 > 其它

中国(北方)大学生程序设计训练赛(第一周) (D E)

2017-03-05 17:05 295 查看
比赛链接

D题是个二分,每次check复杂度为O(n),类似于xdu_1068,只是一个是求积,一个是求商

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;

LL n,ans;
const int N=6;
const LL mod=1e9+7;
LL b[]= {0,0,0,1,0,-1};

struct Mat
{
LL mat

;
} A;
Mat Mut(Mat a,Mat b)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
for(int k=0; k<N; k++)
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j]%mod;
c.mat[i][j]=c.mat[i][j]%mod;
}
return c;
}
Mat Qpow(Mat a,LL n)
{
Mat c;
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
c.mat[i][j]=(i==j);
for(; n; n>>=1)
{
if(n&1) c=Mut(c,a);
a=Mut(a,a);
}
return c;
}
LL cal(Mat A,LL n,LL b[])
{
Mat A_=Qpow(A,n-1);
LL ret=0;
for(int i=0; i<N; i++)
{
ret+=A_.mat[1][i]*b[i];
ret%=mod;
}
return (ret+mod)%mod;
}
void init_A()
{
memset(A.mat,0,sizeof(A.mat));
A.mat[0][0]=1,A.mat[0][1]=1,A.mat[0][2]=1;
A.mat[1][0]=1;
A.mat[2][5]=1;
A.mat[3][2]=1;
A.mat[4][3]=1;
A.mat[5][4]=1;
}
int main()
{
init_A();
while(cin>>b[1]>>b[0]>>n)    //b[1]即f1,b[0]即f2
{
ans=cal(A,n,b);
cout<<ans<<endl;
}
}


E——plus
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐