您的位置:首页 > 其它

hdu 5171(矩阵快速幂,递推)

2016-07-18 21:04 337 查看

GTY's birthday gift

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1286 Accepted Submission(s): 502


[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]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).

[align=left]Sample Input[/align]

3 2
3 6 2

[align=left]Sample Output[/align]

35

[align=left]Source[/align]
BestCoder Round #29

题意:给出一个集合s,集合中的有个初始元素集合,现在的规则是从这些元素中选出两个最大的分别为 a,b,将 a+b 重新添加进集合,然后依次操作 k 次,问最终的元素集合的和。
例如 : 3 6 2 --> 9 6 3 2 --> 15 9 6 3 2 操作两次之后结果为 35
题解:从题目中我们可以知道每次选出的两个数构成了斐波拉契数列,得到递推式 f[i] = f[i-1]+f[i-2],然后我们可以看出第i步求出的和为 sum[i] = sum[i-1]+f[i+1] = sum[i]+f[i]+f[i-1] 我们要求的最终结果是sum[k] 所以我们可以构造矩阵来做.
构造矩阵的方法如下(图丑死了。。。别怪我):



解出 a - j 即可。
(sum[i],f[i+1],f[i]) = ((1,1,1),(0,1,1),(0,1,0))*(sum[i-1],f[i],f[i-1])
所以此递推式的特征矩阵为 M = ((1,1,1),(0,1,1),(0,1,0))
然后求出 A = M^k
最终结果为 A[0][0]*sum[1]+A[0][1]*f[2]+A[0][2]*f[1]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const LL mod = 10000007;
const int N = 100005;
struct Matrix
{
LL v[3][3];
Matrix(){memset(v,0,sizeof(v));}
}ori;
LL n,k,a
;
Matrix mult(Matrix a,Matrix b){
Matrix temp;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
temp.v[i][j] = (temp.v[i][j]+(a.v[i][k]*b.v[k][j])%mod)%mod;
}
}
}
return temp;
}
Matrix pow_mod(Matrix a,LL n){
Matrix ans;
for(int i=0;i<3;i++){
ans.v[i][i] = 1;
}
while(n){
if(n&1) ans = mult(ans,a);
a = mult(a,a);
n>>=1;
}
return ans;
}
int main()
{
ori.v[0][0]=1,ori.v[0][1]=1,ori.v[0][2]=1;
ori.v[1][0]=0,ori.v[1][1]=1,ori.v[1][2]=1;
ori.v[2][0]=0,ori.v[2][1]=1,ori.v[2][2]=0;
while(scanf("%lld%lld",&n,&k)!=EOF)
{
LL max1=-1,max2=-1,sum=0;
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
if(a[i]>max1){
max2 = max1;
max1 = a[i];
}else if(a[i]>max2&&a[i]<=max1){
max2 = a[i];
}
sum+=a[i];
}
//  printf("%lld %lld %lld\n",sum,max1,max2);
Matrix A = pow_mod(ori,k);
LL ans = (A.v[0][0]*sum%mod+A.v[0][1]*max1%mod+A.v[0][2]*max2%mod)%mod;
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: