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 (
).
).
【Sample Input】
3 2 3 6 2
【Sample Output】
35
【题意】
按照规则扩展一个集合k次,然后求其总和。
【分析】
扩展规则很简单,就是一个斐波那契数列,但是如果按照模拟的方法手动推算,复杂度对于本题的数据范围来说是不太合适的。
可以利用矩阵快速幂来迅速完成。
[1,0,0] [S n-1,F n,F n-1] * [1,1,1] =[S n,F n+1,F n] [1,1,0]
剩下要注意的就是数据范围要开到long long了,因为可能涉及到10^9 * 10^9这样的数量级。
1 /* *********************************************** 2 MYID : Chen Fan 3 LANG : G++ 4 PROG : HDU5171 5 ************************************************ */ 6 7 #include <iostream> 8 #include <cstdio> 9 #include <cstring> 10 #include <algorithm> 11 12 #define MOD 10000007 13 14 using namespace std; 15 16 typedef struct matrixnod 17 { 18 long long m[3][3]; 19 } matrix; 20 21 matrix ex= 22 { 23 1,0,0, 24 1,1,1, 25 1,1,0 26 }; 27 28 matrix mat(matrix a,matrix b) 29 { 30 matrix c; 31 for (int i=0;i<3;i++) 32 for (int j=0;j<3;j++) 33 { 34 c.m[i][j]=0; 35 for (int k=0;k<3;k++) c.m[i][j]+=(a.m[i][k]*b.m[k][j])%MOD; 36 c.m[i][j]%=MOD; 37 } 38 return c; 39 } 40 41 matrix mat2(matrix a,matrix b) 42 { 43 matrix c; 44 for (int j=0;j<3;j++) 45 { 46 c.m[0][j]=0; 47 for (int k=0;k<3;k++) c.m[0][j]+=(a.m[0][k]*b.m[k][j])%MOD; 48 c.m[0][j]%=MOD; 49 } 50 return c; 51 } 52 53 matrix doexpmat(matrix b,int n) 54 { 55 matrix a= 56 { 57 1,0,0, 58 0,1,0, 59 0,0,1 60 }; 61 while(n) 62 { 63 if (n&1) a=mat(a,b); 64 n=n>>1; 65 b=mat(b,b); 66 } 67 return a; 68 } 69 70 int main() 71 { 72 int n,k; 73 int a[100010]; 74 while(scanf("%d%d",&n,&k)==2) 75 { 76 long long sum=0; 77 for (int i=1;i<=n;i++) 78 { 79 scanf("%d",&a[i]); 80 sum=(sum+a[i])%MOD; 81 } 82 sort(&a[1],&a[n+1]); 83 matrix start; 84 start.m[0][0]=0; 85 start.m[0][1]=a[n]; 86 start.m[0][2]=a[n-1]; 87 start=mat2(start,doexpmat(ex,k)); 88 89 sum=(sum+start.m[0][0])%MOD; 90 printf("%lld ",sum); 91 } 92 93 return 0; 94 }