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): 1080    Accepted Submission(s): 413


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
BestCoder Round #29
 

问题描述
 GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b (a,b\in S)a+b(a,bS),现在GTY想最大化可重集的和,这个工作就交给你了。
  注:可重集是指可以包含多个相同元素的集合
输入描述
多组数据(约3组),每组数据的第一行有两个数n,k(2 \leq n \leq 100000,1 \leq k \leq 1000000000)n,k(2n100000,1k1000000000) 表示初始元素数量和可使用的魔法数,第二行包含n个数a(1 \leq a_i \leq 100000)a(1ai100000)表示初始时可重集的元素
输出描述
对于每组数据,模10000007输出可重集可能的最大和。
输入样例
3 2
3 6 2
输出样例
35

解题思路:对于每次使用魔法,只需要选取当前序列a[]中最大的两个数a和b,加和放入序列a[]组成新序列a[]。

对于开始的序列a[],选取最大的两个数a和b(假设a>b),那么第一次使用魔法后新序列的最大的两个数为(a+b)和a。

HDU 5171 GTY's birthday gift(矩阵快速幂)_第1张图片

HDU 5171 GTY's birthday gift(矩阵快速幂)_第2张图片

HDU 5171 GTY's birthday gift(矩阵快速幂)_第3张图片

代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 10000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
struct Matrix
{
    ll a[5][5];
    Matrix()
    {
        memset(a,0,sizeof(a));
    }
};
Matrix operator *(Matrix a,Matrix b)
{
    Matrix c;
    int i,j,k;
    memset(c.a,0,sizeof(c.a));
    for(k=0;k<3;k++)
    {
        for(i=0;i<3;i++)
        {
            if(a.a[i][k]==0)
                continue;
            for(j=0;j<3;j++)
            {
                if(b.a[k][j]==0)
                    continue;
                c.a[i][j]=c.a[i][j]+a.a[i][k]*b.a[k][j]%mod;
                c.a[i][j]%=mod;
            }
        }
    }
    return c;
}
Matrix operator ^(Matrix a,int k)
{
    Matrix c;
    for(int i=0;i<3;i++)
        c.a[i][i]=1;
    while(k)
    {
        if(k&1)
            c=c*a;
        a=a*a;
        k>>=1;
    }
    return c;
}
ll a[100005];
int main()
{
    ll i,j,k,n,t;
    while(~scanf("%I64d%I64d",&n,&k))
    {
        memset(a,0,sizeof(a));
        ll ans=0;
        for(i=0;i



你可能感兴趣的:(数论)