HDU 5171 GTY's birthday gift 矩阵快速幂
2015-02-09 12:00
417 查看
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
相关文章推荐
- hdu 5171 GTY's birthday gift(数学,矩阵快速幂)
- HDU 5171 GTY's birthday gift 矩阵快速幂
- hdu 5171 GTY's birthday gift (矩阵快速幂求类斐波那契数列)
- hdu 5171 GTY's birthday gift【矩阵快速幂】【思维】【感受矩阵和数论的神奇】
- HDU 5171 GTY's birthday gift(矩阵快速幂)
- 【矩阵】HDU 5171 GTY's birthday gift 快速幂
- HDU 5171 GTY's birthday gift(矩阵快速幂模板)
- hdu 5171 GTY's birthday gift(矩阵快速幂,斐波那契)
- HDU 5171 GTY's birthday gift (矩阵快速幂)
- hdu 5171 GTY's birthday gift(矩阵快速幂)
- hdu 5171-GTY's birthday gift(矩阵快速幂)
- HDU 5171 GTY's birthday gift (矩阵快速幂)
- HDU 5171 GTY's birthday gift(矩阵快速幂)
- HDU - 5171 GTY's birthday gift (矩阵快速幂)
- BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)
- hdu 5171 GTY's birthday gift (构造矩阵)
- GTY's birthday gift【矩阵快速幂】
- hdu 5171 GTY's birthday gift
- BC#29A:GTY's math problem(math) B:GTY's birthday gift(矩阵快速幂)
- hdu 5171 矩阵快速幂