GTY's birthday gift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 554 Accepted Submission(s): 232
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+b,a
从而可以得出两个数组之间的关系 构造一个三维矩阵
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10000007
#define MAXN 100010
#define INF 0x7fffffff
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
ll aa[MAXN];
struct matrix
{
ll a[3][3];
};
matrix ori,res;
void init()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
res.a[i][j]=(i==j);
}
}
matrix multiply(matrix x,matrix y)
{
matrix temp;
memset(temp.a,0,sizeof(temp.a));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
{
temp.a[i][j]+=x.a[i][k]*y.a[k][j];
temp.a[i][j]%=MOD;
}
return temp;
}
void calc(ll n)
{
while(n)
{
if(n&1) res=multiply(ori,res);
n>>=1;
ori=multiply(ori,ori);
}
}
int main()
{
ll n,k;
while(scanf("%I64d%I64d",&n,&k)!=EOF)
{
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%I64d",&aa[i]);
sum+=aa[i];
sum%=MOD;
}
sort(aa,aa+n);
ll b=aa[n-1];
ll c=aa[n-2];
init();
ori.a[0][0]=ori.a[0][1]=ori.a[0][2]=ori.a[1][1]=ori.a[1][2]=ori.a[2][1]=1;
ori.a[1][0]=ori.a[2][0]=ori.a[2][2]=0;
calc(k);
// for(int i=0;i<3;i++)
// {
// for(int j=0;j<3;j++)
// {
// printf("%d ",res.a[i][j]);
// }
// cout<<endl;
// }
ll ans=0;
ans+=(res.a[0][0]*sum)%MOD+(res.a[0][1]*b)%MOD+(res.a[0][2]*c)%MOD;
printf("%I64d\n",ans%MOD);
}
return 0;
}
//每次从数列中挑选最大的两个值相加 求得的和再加入到数列中
//|1 1 1| |S(n-1)| |S(n) |
//|0 1 1| * |F(n-1)| = |F(n) |
//|0 1 0| |F(n-2)| |F(n-1)|
//S(n)表示现在数组中所有数的和 F(n)表示现在数组中最大的值