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

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,bS ), 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 ( 2n100000,1k1000000000 ). The second line contains n elements ai ( 1ai100000 )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

 

题目大意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b ,现在GTY想最大化可重集的和,这个工作就交给你了。 注:可重集是指可以包含多个相同元素的集合

 

AC代码:

<p>#include "stack"
#include "cstdio"
#include "iostream"
#include "cmath"
#include "set"
#include "sstream"
#include "cctype"
#include "string"
#include "cstring"
#include "algorithm"
#include "queue"
#include "map"
using namespace std;
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
#define MOD 10000007
#define maxn 200000</p><p>using namespace std;</p>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[1][1]=u.m[1][2]=u.m[2][1]=1;
                v.m[0][0]=v.m[1][0]=v.m[2][0]=1;
                u=pow(u,y);
                v=u*v;
                printf("%I64d\n",(c*v.m[0][0]+d*v.m[1][0]+sum-c-d+MOD)%MOD);
        }
        return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值