HDU5171(矩阵快速幂)

来源:互联网 发布:网络用语 夯是什么意思 编辑:程序博客网 时间:2024/06/05 15:03

题意:给你一个序列长度为n(n<=100000),在给你一个k(k<10^9)表示可以使此序列依次增加k个数,第i次增加的数是第i-1次时数列里任意两个数之和。求增加k个数后数列的最大和。mod10000007

分析:A = {{1,1,0},{1,0,0},{1,0,1}},B = {{4,2,1},{2,1,1},{0,0,0}}。设原数列中最大的数为a,次大的数为b,baa+b2a+b3a+2b5a+3b分析k12345a12358b11235sigma(a)1361119sigma(b)124712

所以找到规律 分情况 k<=3时和k>3时,实在太困了!不想写了!



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define MOD 10000007#define N 100009typedef long long LL;const int A[3][3] = {{1,1,0},{1,0,0},{1,0,1}};const int B[3][3] = {{4,2,1},{2,1,1},{0,0,0}};int num[N];struct Ex{    LL a[5][5];    Ex(){}    Ex(LL a11,LL a12,LL a13,LL a21,LL a22,LL a23,LL a31,LL a32,LL a33){        a[1][1] = a11; a[1][2] = a12; a[1][3] = a13;        a[2][1] = a21; a[2][2] = a22; a[2][3] = a23;        a[3][1] = a31; a[3][2] = a32; a[3][3] = a33;    }};Ex mul(Ex x,Ex y){    Ex ans;    for(int i = 1;i<=3;i++)    for(int j = 1;j<=3;j++){        ans.a[i][j] = 0;        for(int k = 1;k<=3;k++)            ans.a[i][j] = (ans.a[i][j] + x.a[i][k]*y.a[k][j]%MOD)%MOD;    }    return ans;}Ex pow(int n){    int a00 = A[0][0],a01=A[0][1],a02 = A[0][2];    int a10 = A[1][0],a11=A[1][1],a12 = A[1][2];    int a20 = A[2][0],a21=A[2][1],a22 = A[2][2];    Ex tem = Ex(a00,a01,a02,a10,a11,a12,a20,a21,a22);    Ex ans = Ex(1,0,0,0,1,0,0,0,1);    while(n){        if(n&1) ans = mul(ans,tem);        tem = mul(tem,tem);        n>>=1;    }    return ans;}int main(){    int n,k;    int f[5] = {0,1,2,4,7};    while(~scanf("%d%d",&n,&k)){        LL sum = 0;        for(int i = 1;i<=n;i++){            scanf("%d",&num[i]);            sum += num[i];        }        sort(num+1,num+n+1);        Ex ans;        Ex tem = Ex(B[0][0],B[0][1],B[0][2],B[1][0],B[1][1],B[1][2],B[2][0],B[2][1],B[2][2]);        if(k<=3) ans = Ex(f[k+1],f[k],0,0,0,0,0,0,0);        else     ans = mul(tem,pow(k-2));        LL b = num[n-1]%MOD,a = num[n]%MOD;        LL res = (sum + ans.a[1][2]*b%MOD+ans.a[1][1]*a%MOD-a+MOD)%MOD;        printf("%I64d\n",res);    }    return 0;}




0 0
原创粉丝点击