hdu 5171 GTY's birthday gift(快速幂4)

GTY's birthday gift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 818    Accepted Submission(s): 330


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
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5209  5208  5207  5206  5205 
 找个机会把上面的题做了,练练。
解析:题意是给你N个数字,让你用这些数字构造一个最大的集合,其中可以选取M次最大的俩个数加入集合中,求最后集合中的数最大。
通过分析题目,设N个数中最大的数是a,b(a>b)。可以看到第一次加入a+b,第二次加入a+b+a,第三次加入3a+2b。然后找规律,得到矩阵,求和。
以上是我的分析。下面是一位大牛的分析:
显然每次会从可重集中选择最大的两个进行操作,设这两数为 a,b(a>=b) ,操作之后的数一定是操作后集合中最大的,下一次选取的数一定是 a+b a ,这就形成了一个类似于斐波那契数列的东西,矩阵乘法快速幂求前n项和即可,转移矩阵如下
1 0 1     sum
0 1 1  *  a+b 
0 1 0      a
设矩阵A为
1 0 1
0 1 1
0 1 0
B为
sum
a+b
a
 
  
ans=A^k*B,接着矩阵快速幂



#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define mod 10000007
 struct Matrix
{
   long long ma[5][5];
};
Matrix multi(Matrix a,Matrix b)
{
    Matrix ans;
    memset(ans.ma,0,sizeof(ans.ma));
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
    {

        for(int k=1;k<=3;k++)
           ans.ma[i][k]=(ans.ma[i][k]+(a.ma[i][j]*b.ma[j][k])%mod)%mod;
    }return ans;
}
int main()
{
    long long  n,k,a1[100001];
    while(cin>>n>>k)
    {
        long long sum=0;
        for(int i=0;i<n;i++)
            {cin>>a1[i];sum+=a1[i];sum%=mod;}
            sort(a1,a1+n);

       Matrix a,b,ans;
       memset(ans.ma,0,sizeof(ans.ma));
        for(int i=1;i<=3;i++)
            for(int j=1;j<=3;j++)
            if(i==j)ans.ma[i][j]=1;
        memset(a.ma,0,sizeof(a.ma));
        a.ma[1][1]=a.ma[2][2]=a.ma[1][2]=a.ma[2][3]=a.ma[3][2]=1;
        while(k)
        {
            if(k&1)
                ans=multi(a,ans);
            a=multi(a,a);
            k>>=1;
        }
        memset(b.ma,0,sizeof(b.ma));
        b.ma[1][1]=sum;
        b.ma[2][1]=a1[n-1]+a1[n-2];
        b.ma[3][1]=a1[n-1];
        ans=multi(ans,b);
        cout<<ans.ma[1][1]<<endl;
    }return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值