HDU5171 GTY's birthday gift —— 矩阵快速幂
来源:互联网 发布:群排名优化 编辑:程序博客网 时间:2024/05/21 19:43
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5171
GTY's birthday gift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1654 Accepted Submission(s): 649
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
3 23 6 2
Sample Output
35
题解:
可以推出斐波那契数列,那么就用矩阵快速幂求前n项,以及前n项和。
代码如下:
#include <bits/stdc++.h>#define rep(i,s,t) for(int (i)=(s); (i)<=(t); (i)++)#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const double eps = 1e-6;const int mod = 10000007;const int maxn = 100000+10;int n,k;int a[maxn];struct MAT{ LL mat[5][5]; void init() { rep(i,1,3) rep(j,1,3) mat[i][j] = (i==j); }};MAT mul(MAT x, MAT y){ MAT s; ms(s.mat,0); rep(i,1,3) rep(j,1,3) rep(k,1,3) s.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%mod, s.mat[i][j] %= mod; return s;}MAT qpow(MAT x, int y){ MAT s; s.init(); while(y) { if(y&1) s = mul(s,x); x = mul(x,x); y >>= 1; } return s;}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { rep(i,1,n) scanf("%lld",&a[i]); sort(a+1,a+1+n); LL ans = 0; rep(i,1,n) ans += a[i], ans %= mod; MAT s; ms(s.mat,0); s.mat[1][1] = s.mat[1][2] = s.mat[1][3] = 1; s.mat[2][2] = s.mat[2][3] = s.mat[3][2] = 1; s = qpow(s,k-2); ans += (1LL*(2*a[n-1]+3*a[n])*s.mat[1][1])%mod, ans %= mod; ans += (1LL*(1*a[n-1]+2*a[n])*s.mat[1][2])%mod, ans %= mod; ans += (1LL*(1*a[n-1]+1*a[n])*s.mat[1][3])%mod, ans %= mod; cout<<ans<<endl; }}
阅读全文
0 0
- HDU5171 GTY's birthday gift —— 矩阵快速幂
- HDU5171 GTY's birthday gift(矩阵快速幂)
- hdu5171---GTY's birthday gift
- hdu5171 GTY's birthday gift
- hdu5171 GTY's birthday gift 矩阵快速幂求斐波那契前n项和,矩阵快速幂模板
- 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(矩阵快速幂)
- bc 29 GTY's birthday gift(矩阵快速幂)
- object-position
- Python开发环境搭建 --- Eclipse + PyDev + Anaconda
- 吃糖果——抽屉原理
- Linux基础命令学习
- 51nod1060(最复杂的数)
- HDU5171 GTY's birthday gift —— 矩阵快速幂
- 【怎样写代码】工厂三兄弟之抽象工厂模式(六):扩展案例II
- 网站外链代码编写
- 集合框架——Set(TreeSet)
- js的三种接口(详细解释)
- AMD规范创建一个模块对象与模块类
- BN
- 剑指offer面试题[26]-复杂链表的复制
- 【HDU-1286】找新朋友