HDU5171 GTY's birthday gift(矩阵快速幂)
2015-02-19 16:33
288 查看
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).
Sample Input
Sample Output
Source
BestCoder Round #29
Recommend
参考大神博客 :http://blog.csdn.net/crescent__moon/article/details/43617703
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).
Sample Input
3 2 3 6 2
Sample Output
35
Source
BestCoder Round #29
Recommend
参考大神博客 :http://blog.csdn.net/crescent__moon/article/details/43617703
//需要知道的一点是斐波拉契数列 s(n)=a(n+2)-1; #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; #define mod 10000007 #define N 100005 typedef __int64 ll; struct mat{ ll a[2][2]; mat(){memset(a,0,sizeof(a));} mat operator *(mat b) { mat c; for(int i=0;i<2;i++) for(int j=0;j<2;j++) { c.a[i][j]=0; for(int k=0;k<2;k++) c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j]%mod)%mod; } return c; } }; ll ans; int a ; mat s,e; void inint() { s.a[0][0]=s.a[0][1]=s.a[1][0]=1; s.a[1][1]=0; int i,j; e.a[0][1]=e.a[1][0]=0; for(i=0;i<2;i++) e.a[i][i]=1; } mat powmul(int k) { int i,j; while(k) { if(k&1) { e=e*s; } k>>=1; s=s*s; } return e; } int main() { int i,j; ll k,n; while(~scanf("%I64d%I64d",&n,&k)) { ans=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); ans=(ans+a[i])%mod; } inint(); sort(a,a+n); s=powmul(k+2); ans=(ans+(s.a[0][0]-2)*a[n-1]%mod)%mod; //好好看看上面的话,列出数列 ans=(ans+(s.a[0][1]-1)*a[n-2]%mod)%mod; printf("%I64d\n",ans); } return 0; }
相关文章推荐
- hdu5171 GTY's birthday gift 矩阵快速幂求斐波那契前n项和,矩阵快速幂模板
- bc 29 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&#39;s math problem(快速幂(对数法))、B--GTY&#39;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 (矩阵快速幂求类斐波那契数列)
- 【Best Coder】#29 B GTY's birthday gift(快速幂|mod的时候记得负!)
- 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)
- GTY's birthday gift(矩阵快速幂)
- HDU 5171 GTY's birthday gift(矩阵快速幂)
- hdu 5171-GTY's birthday gift(矩阵快速幂)
- HDU 5171 GTY's birthday gift (矩阵快速幂)