GTY's birthday gift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
【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![HDU 5171 GTY's birthday gift 矩阵快速幂 HDU 5171 GTY's birthday gift 矩阵快速幂](/default/index/img?u=aHR0cHM6Ly9iYnNtYXguaWthZmFuLmNvbS9zdGF0aWMvTDNCeWIzaDVMMmgwZEhCekwzZDNkeTVqYm1Kc2IyZHpMbU52YlM4PS5qcGc=)
).
).
【Sample Input】
【Sample Output】
【题意】
按照规则扩展一个集合k次,然后求其总和。
【分析】
扩展规则很简单,就是一个斐波那契数列,但是如果按照模拟的方法手动推算,复杂度对于本题的数据范围来说是不太合适的。
可以利用矩阵快速幂来迅速完成。
[1,,]
[S n-,F n,F n-] * [,,] =[S n,F n+,F n]
[,,]
剩下要注意的就是数据范围要开到long long了,因为可能涉及到10^9 * 10^9这样的数量级。
/* ***********************************************
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[][];
} matrix; matrix ex=
{
,,,
,,,
,,
}; matrix mat(matrix a,matrix b)
{
matrix c;
for (int i=;i<;i++)
for (int j=;j<;j++)
{
c.m[i][j]=;
for (int k=;k<;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=;j<;j++)
{
c.m[][j]=;
for (int k=;k<;k++) c.m[][j]+=(a.m[][k]*b.m[k][j])%MOD;
c.m[][j]%=MOD;
}
return c;
} matrix doexpmat(matrix b,int n)
{
matrix a=
{
,,,
,,,
,,
};
while(n)
{
if (n&) a=mat(a,b);
n=n>>;
b=mat(b,b);
}
return a;
} int main()
{
int n,k;
int a[];
while(scanf("%d%d",&n,&k)==)
{
long long sum=;
for (int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum=(sum+a[i])%MOD;
}
sort(&a[],&a[n+]);
matrix start;
start.m[][]=;
start.m[][]=a[n];
start.m[][]=a[n-];
start=mat2(start,doexpmat(ex,k)); sum=(sum+start.m[][])%MOD;
printf("%lld\n",sum);
} return ;
}