2 1 4 100 2 0 4 100
21 12
让我们求
sn= f(b)+ f(k+b)+ f(2k+b)+……+ f(nk+b).
运用以上方法,矩阵连乘,解决问题!
(我的代码求别改,这段代码好不容易才改对的,本来的代码老是ce!!!改成这样我也不造哪里错了又改对了,求大牛指导)
#include<iostream> #include<stdio.h> using namespace std; const int max0=2; const int max1=4; typedef long long ll; int mod; typedef struct{ long long zj[max0][max0]; } matrix; typedef struct{ long long zj[max1][max1]; } matrix1; matrix I={1,0,0,1}; matrix j2={0,1,1,1}; matrix1 jj1={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; matrix1 jj2={0,0,1,0, 0,0,0,1, 0,0,1,0, 0,0,0,1}; matrix lc1(matrix a,matrix b) { matrix c; for(int i=0;i<max0;i++) { for(int j=0;j<max0;j++) { c.zj[i][j]=0; for(int k=0;k<max0;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c; } matrix a,b; matrix1 a0,b0; matrix quick1(ll n) { a=j2,b=I; while(n>=1) { if(n&1) b=lc1(b,a); n>>=1; a=lc1(a,a); } return b; } matrix1 lc2(matrix1 a,matrix1 b) { matrix1 c; for(int i=0;i<max1;i++) { for(int j=0;j<max1;j++) { c.zj[i][j]=0; for(int k=0;k<max1;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c; } matrix1 quick2(ll n) { a0=jj2,b0=jj1; while(n>=1) { if(n&1) b0=lc2(b0,a0); n>>=1; a0=lc2(a0,a0); } return b0; } int main() { ll k,bbb,n; while(cin>>k>>bbb>>n>>mod) { matrix tm1=quick1(k); matrix tm2=quick1(bbb); jj2.zj[0][0]=tm1.zj[0][0],jj2.zj[0][1]=tm1.zj[0][1],jj2.zj[1][0]=tm1.zj[1][0],jj2.zj[1][1]=tm1.zj[1][1]; matrix1 tm=quick2(n); long long tmp=(tm2.zj[0][0]%mod*tm.zj[0][3]%mod)%mod+(tm2.zj[0][1]%mod*tm.zj[1][3]%mod)%mod; tmp=(tmp+mod)%mod; cout<<tmp<<endl; } return 0; }
#include<iostream> #include<stdio.h> using namespace std; const int max0=2; const int max1=4; typedef long long ll; int mod; typedef struct{ long long zj[max0][max0]; } matrix; typedef struct{ long long zj[max1][max1]; } matrix1; matrix j0={1,0,0,1}; matrix j2={0,1,1,1}; matrix1 jj1={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; matrix1 jj2={0,0,1,0, 0,0,0,1, 0,0,1,0, 0,0,0,1}; matrix lc1(matrix a,matrix b) { matrix c; for(int i=0;i<max0;i++) { for(int j=0;j<max0;j++) { c.zj[i][j]=0; for(int k=0;k<max0;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c; } matrix quick1(ll n) { matrix a=j2,b=j0; while(n>=1) { if(n&1) b=lc1(b,a); n>>=1; a=lc1(a,a); } return b; } matrix1 lc2(matrix1 a,matrix1 b) { matrix1 c; for(int i=0;i<max1;i++) { for(int j=0;j<max1;j++) { c.zj[i][j]=0; for(int k=0;k<max1;k++) c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod; c.zj[i][j]%=mod; } } return c; } matrix1 quick2(ll n) { matrix1 a=jj2,b=jj1; while(n>=1) { if(n&1) b=lc2(b,a); n>>=1; a=lc2(a,a); } return b; } int main() { ll k,bbb,n; while(cin>>k>>bbb>>n>>mod) { matrix tm1=quick1(k); matrix tm2=quick1(bbb); jj2.zj[0][0]=tm1.zj[0][0],jj2.zj[0][1]=tm1.zj[0][1],jj2.zj[1][0]=tm1.zj[1][0],jj2.zj[1][1]=tm1.zj[1][1]; matrix1 tm=quick2(n); long long tmp=(tm2.zj[0][0]%mod*tm.zj[0][3]%mod)%mod+(tm2.zj[0][1]%mod*tm.zj[1][3]%mod)%mod; tmp=(tmp+mod)%mod; cout<<tmp<<endl; } return 0; }