您的位置:首页 > 其它

HDU 5171 GTY's birthday gift 矩阵快速幂

2015-02-09 12:00 351 查看

GTY's birthday gift

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

[align=left]【Problem Description】[/align]
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.
[align=left] [/align]
[align=left]【Input】[/align]
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 .

[align=left]【Output】[/align]
For each case , print the maximum sum of the multiset (mod 10000007

/* ***********************************************
MYID    : Chen Fan
LANG    : G++
PROG    : HDU5171
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MOD 10000007

using namespace std;

typedef struct matrixnod
{
long long m[3][3];
} matrix;

matrix ex=
{
1,0,0,
1,1,1,
1,1,0
};

matrix mat(matrix a,matrix b)
{
matrix c;
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
{
c.m[i][j]=0;
for (int k=0;k<3;k++) c.m[i][j]+=(a.m[i][k]*b.m[k][j])%MOD;
c.m[i][j]%=MOD;
}
return c;
}

matrix mat2(matrix a,matrix b)
{
matrix c;
for (int j=0;j<3;j++)
{
c.m[0][j]=0;
for (int k=0;k<3;k++) c.m[0][j]+=(a.m[0][k]*b.m[k][j])%MOD;
c.m[0][j]%=MOD;
}
return c;
}

matrix doexpmat(matrix b,int n)
{
matrix a=
{
1,0,0,
0,1,0,
0,0,1
};
while(n)
{
if (n&1) a=mat(a,b);
n=n>>1;
b=mat(b,b);
}
return a;
}

int main()
{
int n,k;
int a[100010];
while(scanf("%d%d",&n,&k)==2)
{
long long sum=0;
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum=(sum+a[i])%MOD;
}
sort(&a[1],&a[n+1]);
matrix start;
start.m[0][0]=0;
start.m[0][1]=a
;
start.m[0][2]=a[n-1];
start=mat2(start,doexpmat(ex,k));

sum=(sum+start.m[0][0])%MOD;
printf("%lld\n",sum);
}

return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: