HDU 5171
这道题本来很水,以前做过一样的,斐波那契数列,用矩阵快速幂的方法求,本来很水,以前做过很多次,为毛做的时候没想到T_T
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #define LL __int64 using namespace std; const LL MOD=10000007; int a[100005]; struct Matrix{ LL p[2][2]; }; Matrix per,s; LL tmp[2]; Matrix operator *(Matrix a,Matrix b){ Matrix c; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ c.p[i][j]=0; for(int k=0;k<2;k++) c.p[i][j]=(c.p[i][j]+a.p[i][k]*b.p[k][j])%MOD; } } return c; } Matrix cal_quick(int k){ Matrix ans=per,p=s; while(k){ if(k&1) ans=ans*p; k>>=1; p=p*p; } return ans; } int main(){ per.p[0][0]=per.p[1][1]=1; per.p[0][1]=per.p[1][0]=0; s.p[0][0]=s.p[0][1]=s.p[1][0]=1; s.p[1][1]=0; int n,k; LL first,second,pos; LL ans; while(scanf("%d%d",&n,&k)!=EOF){ ans=0; first=second=pos=-1; for(int i=0;i<n;i++){ scanf("%I64d",&a[i]); ans=(ans+a[i])%MOD; if(a[i]>first){ first=a[i]; pos=i; } } ans-=first; for(int i=0;i<n;i++){ if(pos!=i&&second<a[i]) second=a[i]; } tmp[0]=1;tmp[1]=0; Matrix one=cal_quick(k+1); LL ans_a=(one.p[0][0]*tmp[0]+one.p[0][1]*tmp[1])%MOD; ans=((ans+((ans_a-1)*second)%MOD)%MOD+MOD)%MOD; one=one*s; ans_a=(one.p[0][0]*tmp[0]+one.p[0][1]*tmp[1])%MOD; ans=((ans+((ans_a-1)*first)%MOD)%MOD+MOD)%MOD; printf("%I64d\n",ans); } return 0; }