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): 1186    Accepted Submission(s): 456


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:   5639  5638  5637  5636  5635 
 


      题意:已经存在一个长度为n的序列,现在可以任意从中找到两个数,把它们的和加入序列,这样的操作执行k次,那么这个序列总和最大可以是多少。

      分析可得,每次找到次大值a,最大值b,加入a+b会让和sum最大,

那么依次要加入的数为a+b,a+2b,2a+3b,3a+5b,5a+8b。。。

可以看出a的系数分别是1,1,2,3,5,8。。。而b的系数分别是1,2,3,5,8。。。

那么这不就是斐波那契数列吗,我们可以利用矩阵快速幂来求出斐波那契数列的值(理由是斐波那契序列是可以用2*2矩阵的k次方来表示的,想知道详情可以先做一下POJ 3070),但是这道题需要求的是斐波那契的和,而不是第k项,

这里就要用到一个公式sum(k)=F(k+2)-1,那么我们就可以利用快速幂得到第k+2项的值,再减1(a的系数之和xa),第k+3项的值,再减2(b的系数之和xb,因为b的系数少啦一个1,所以需要求出第k+3项的值),最后xa*a+xb*b就是添加的数之和啦,再加上已有的数之和,就是最终答案啦。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5;
const int maxm = 100010;
const int MOD = 10000007;
int N, K, a[maxm];
struct Matrix
{
    long long mat[maxn][maxn];
    Matrix()        //构造函数
    {
        memset(mat, 0, sizeof(mat));
    }

    Matrix operator * (Matrix A)    //重载运算符*
    {
        Matrix res;
        for (int i = 0; i < 2; i++) //矩阵乘法
        {
            for (int j = 0; j < 2; j++)
            {
                for (int k = 0; k < 2; k++)
                    res.mat[i][j] = (res.mat[i][j] + (mat[i][k] * A.mat[k][j]) % MOD) % MOD;
            }
        }
        return res;
    }
};
//快速求矩阵A^n
Matrix pow_mul(Matrix A, int n)
{
    Matrix res;
    //一个矩阵的0次方为主对角线全为1,其他全为0的矩阵
    for (int i = 0; i < maxn; i++) res.mat[i][i] = 1;
    while (n)
    {
        if (n & 1) res = res * A;   //若n为奇数
        A = A * A;
        n >>= 1;                    //n = n / 2
    }
    return res;
}

int main()
{
    while (~scanf("%d%d", &N, &K))
	{
		for (int i = 1; i <= N; i++) scanf("%d", &a[i]);
		
		sort(a + 1, a + N + 1);
		Matrix A;
       	long long xa, xb;
       	
        A.mat[0][0] = A.mat[0][1] = A.mat[1][0] = 1;
        xa = pow_mul(A, K + 2).mat[0][1] - 1;
        xb = pow_mul(A, K + 3).mat[0][1] - 2;
        
        long long ans = 0;
        ans = (ans + a[N - 1] * xa) % MOD;
        ans = (ans + a[N] * xb) % MOD;

        for (int i = 1; i <= N; i++) ans = (ans + a[i]) % MOD;
        cout << ans << endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值