您的位置:首页 > 其它

HDU 5171 GTY's birthday gift(矩阵快速幂)

2015-02-08 16:26 417 查看
Problem Description

FFZ's birthday is coming. GTY wants to give a gift to ZZF. He asked his gay friends what he should give to ZZF. One of them said, 'Nothing is more interesting than a number multiset.' So GTY decided to make a multiset for ZZF. Multiset can contain elements
with same values. Because GTY wants to finish the gift as soon as possible, he will use JURUO magic. It allows him to choose two numbers a and b(a,b∈S),
and add a+b to
the multiset. GTY can use the magic for k times, and he wants the sum of the multiset is maximum, because the larger the sum is, the happier FFZ will be. You need to help him calculate the maximum sum of the multiset.



Input

Multi test cases (about 3) . The first line contains two integers n and k (2≤n≤100000,1≤k≤1000000000).
The second line contains n elements ai (1≤ai≤100000)separated
by spaces , indicating the multiset S .



Output

For each case , print the maximum sum of the multiset (mod 10000007).



Sample Input

3 2
3 6 2




Sample Output

35



显然经过一系列操作会形成一个斐波那契数列。
考虑矩阵我们有


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int MOD = 10000007;
const int INF=0x3f3f3f3f;
typedef vector<LL>vec;
typedef vector<vec>mat;
LL a[100000+10];
int n,k;
LL sum;
mat mul(mat A,mat B)
{
    mat C(A.size(),vec(B[0].size()));
    REP(i,A.size())
    {
        REP(j,B[0].size())
        {
            REP(k,B.size())
              C[i][j]=(C[i][j]+A[i][k]*B[k][j])%MOD;
        }
    }
    return C;
}
mat pow(mat A,LL n)
{
    mat B(A.size(),vec(A.size()));
    REP(i,A.size())
       B[i][i]=1;
    while(n>0)
    {
        if(n&1)   B=mul(B,A);
        A=mul(A,A);
        n>>=1;
    }
    return B;
}
void work()
{
    mat A(3,vec(3));
    A[0][0]=1;A[0][1]=1;A[0][2]=1;
    A[1][0]=0;A[1][1]=1;A[1][2]=1;
    A[2][0]=0;A[2][1]=1;A[2][2]=0;
    A=pow(A,k);
    printf("%I64d\n",(A[0][0]*(a[n-2]+a[n-1])+A[0][1]*a[n-1]+A[0][2]*a[n-2]+sum-(a[n-1]+a[n-2]))%MOD);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
       sum=0;
       REP(i,n)
       {
           scanf("%I64d",&a[i]);
           sum=(sum+a[i])%MOD;
       }
       sort(a,a+n);
       work();
    }
    return 0;
}


⎡⎣⎢1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: