hdu 5171 GTY's birthday gift (BestCoder Round #29)

GTY‘s birthday gift

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

Total Submission(s): 209    Accepted Submission(s): 71

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 ai (1≤ai≤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

题解:

    显然每次会从可重集中选择最大的两个进行操作,设这两数为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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=10000007;
typedef struct matrix
{
    long long ma[5][5];
}matrix;
matrix multi(matrix x,matrix y)
{
    matrix ans;
    memset(ans.ma,0,sizeof(ans.ma));
   for(int i=1;i<=3;i++)
   {
       for(int j=1;j<=3;j++)
       {
           if(x.ma[i][j])
           for(int k=1;k<=3;k++)
           {
               ans.ma[i][k]=(ans.ma[i][k]+(x.ma[i][j]*y.ma[j][k])%mod)%mod;
           }
       }
   }
   return ans;
}
int main()
{
    long long sum;
    long long n,k;
    while(~scanf("%I64d%I64d",&n,&k))
    {
        sum=0;
        long long t, maxf=0,maxs=0;
        for(int i=0;i<n;i++)
        {
            scanf("%I64d",&t);
            sum=(sum+t)%mod;
            if(t>maxf)
            {
                maxs=maxf;
                maxf=t;
            }
            else if(t>maxs)
            maxs=t;
        }
        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[1][2]=a.ma[2][2]=a.ma[2][3]=a.ma[3][2]=1;
        while(k)
        {
            if(k&1)
            ans=multi(a,ans);
            a=multi(a,a);
            k=k>>1;
        }
        memset(b.ma,0,sizeof(b.ma));
        b.ma[1][1]=sum;
        b.ma[2][1]=maxf+maxs;
        b.ma[3][1]=maxf;
        ans=multi(ans,b);
        printf("%I64d\n",ans.ma[1][1]);
    }
    return 0;
}

??



hdu 5171 GTY's birthday gift (BestCoder Round #29)

时间: 2024-10-04 22:57:46

hdu 5171 GTY's birthday gift (BestCoder Round #29)的相关文章

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as

hdu 5171 GTY&#39;s birthday gift(数学,矩阵快速幂)

题意: 开始时集合中有n个数. 现在要进行k次操作. 每次操作:从集合中挑最大的两个数a,b进行相加,得到的数添加进集合中. 以此反复k次. 问最后集合中所有数的和是多少. (2≤n≤100000,1≤k≤1000000000) 思路: 写出来发现是要求Fibonaci的前n个数的和. Fibonaci是用矩阵快速幂求的,这个也可以. [Sn,Fn,Fn-1]=[某个矩阵]*[Sn-1,Fn-1,Fn-2] [S2,F2,F1]=[2,1,1] 然后写,,, 这个代码有些繁琐,应该把矩阵操作单独

HDU 5171 GTY&#39;s birthday gift(矩阵快速幂 )

HDU 5171 GTY's birthday gift ( 矩阵快速幂裸题目 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 3 #define MOD 10000007 #define clr( a, b ) memset( a, b, sizeof(a) ) typedef long long LL; struct M

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

BestCoder Round #29——A--GTY&#39;s math problem(快速幂(对数法))、B--GTY&#39;s birthday gift(矩阵快速幂)

GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description GTY is a GodBull who will get an Au in NOI . To have more time to learn alg

BestCoder Round #29 1001 GTY&#39;s math problem

GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description GTY is a GodBull who will get an Au in NOI . To have more time to learn alg

BestCoder Round #29 GTY&#39;s gay friends

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int MAX = 100000+10;int res[MAX<<2],ans[MAX],t[MAX];int a[MAX],last[MAX];struct node {    int L,R,id,d;    bool operator <(c

HDU 4932 Miaomiao&#39;s Geometry(BestCoder Round #4)

Problem Description: There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T.2.The le

HDU 5170 GTY&#39;s math problem (bsst code #29 1001)

GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 146    Accepted Submission(s): 80 Problem Description GTY is a GodBull who will get an Au in NOI . To have more time to learn