您的位置:首页 > 其它

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

2015-02-14 11:05 393 查看


GTY's birthday gift

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 408 Accepted Submission(s): 154



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 a

i

(1≤a

i

≤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<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
#define MOD 10000007
#define maxn 200000
using namespace std;
long long y;
struct mat
{
        long long m[3][3];
        mat(){memset(m,0,sizeof(m));}
};
mat operator *  (mat a,mat b)
{
        mat ans;
        for(int i=0;i<=2;i++)
                for(int j=0;j<=2;j++)
                        for(int k=0;k<=2;k++)
                                ans.m[i][j]=(ans.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
        return ans;
}
mat pow(mat a,long long n)
{
        if(n==0){
                memset(a.m,0,sizeof(a));
                for(int i=0;i<=2;i++)a.m[i][i]=1;
        }
        if(n==1)return a;
        if(n&1)return a*pow(a,n-1);
        else{
                mat u=pow(a,n>>1);
                return u*u;
        }
}
long long a[maxn];
int main()
{
        long long x;
        while(scanf("%I64d%I64d",&x,&y)!=EOF)
        {
                long long sum=0;
                for(int i=1;i<=x;i++)
                scanf("%I64d",&a[i]),sum+=a[i];

                sort(a+1,a+1+x);
                long long c=a[x],d=a[x-1];
                mat u,v;
                u.m[0][0]=u.m[0][1]=u.m[0][2]=u.m[1][1]=u.m[1][2]=u.m[2][1]=1;
                v.m[0][0]=sum,v.m[1][0]=c,v.m[2][0]=d;
                u=pow(u,y);
                v=u*v;
                printf("%I64d\n",v.m[0][0]%MOD);
        }
        return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: