hdu 5171 GTY's birthday gift【矩阵快速幂】【思维】【感受矩阵和数论的神奇】
2016-01-26 20:25
661 查看
GTY's birthday gift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1176 Accepted Submission(s): 449
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 2 3 6 2
Sample Output
35
这真是一个神奇的题,更神奇的是矩阵和数论给我带来的惊喜。矩阵能够求斐波那契数,不仅仅是斐波那契数神奇,矩阵也是如此的神奇。
我们从题目中能够推测:
假设a是序列中最大数,b是次大数,那么每一次多出来的是:a+b,2a+b,3a+2b,5a+3b..................
比原序列多出来的值是:a+b,3a+2b,6a+4b....................
其中我们能发现a前系数为1 2 3 5 8...............
b前系数为:1 1 2 3 5 8......................
很明显的斐波那契数列。如果接触过POJ3070的小伙伴可能马上就有思路了。矩阵快速幂求斐波那契数:
这里初始化矩阵为2阶:
1 1
1 0
自乘:
2 1
1 1
再自乘:
3 2
2 1
再自乘:
5 3
3 2
我们发现第一层上边的两个数一直都是斐波那契数。
这个时候我们根据矩阵乘法的特性,加上各种推论能够构造一个矩阵:
1 1 1
0 1 1
0 1 0
自乘之后
1 3 2
0 2 1
0 1 1
无限幂次都能保证第一行的三个元素分别代表: 1(第一个数永远都是1),比原序列多出的a的个数,比原序列多出的b的个数。
然后我们就能写出对应的代码:
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; #define ll long long int int const MOD = 10000007; struct matrix { ll m[3][3]; }; int a[100010]; matrix multiply(matrix x, matrix y) //矩阵乘法 { matrix tmp; memset(tmp.m, 0, sizeof(tmp.m)); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(x.m[i][j] == 0) continue; for(int k = 0; k < 3; k++) { if(y.m[j][k] == 0) continue; tmp.m[i][k] += x.m[i][j] * y.m[j][k] % MOD; tmp.m[i][k] %= MOD; } } } return tmp; } matrix quickmod(matrix a, int n) //矩阵快速幂 { matrix res; memset(res.m, 0, sizeof(res.m)); for(int i = 0; i < 3; i++) res.m[i][i] = 1; while(n) { if(n & 1) res = multiply(res, a); n >>= 1; a = multiply(a, a); } return res; } int main() { int n,k; while(~scanf("%d%d",&n,&k)) { ll sum=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } sort(a,a+n); matrix tmp; tmp.m[0][0] = 1; tmp.m[0][1] = 1; tmp.m[0][2] = 1; tmp.m[1][0] = 0; tmp.m[1][1] = 1; tmp.m[1][2] = 1; tmp.m[2][0] = 0; tmp.m[2][1] = 1; tmp.m[2][2] = 0; tmp = quickmod(tmp, k); printf("%lld %lld %lld\n",tmp.m[0][0],tmp.m[0][1],tmp.m[0][2]); ll output= (tmp.m[0][0] * sum + tmp.m[0][1] * a[n - 1] + tmp.m[0][2] * a[n - 2]) % MOD; printf("%lld\n",output); } }
相关文章推荐
- 图像视觉领域新文章及相应开源代码
- spring整合redis客户端及缓存接口设计(转)
- iOS 根据字符串数目,自定义Label等控件的高度
- 93. Spiral Matrix
- 大型网站架构系列:消息队列(二) (转)
- Hortonworks HDP Sandbox定制(配置)开机启动服务(组件)
- ubuntu server 无线网卡的处理
- leetcode166. Fraction to Recurring Decimal
- Good Bye 2015 D. New Year and Ancient Prophecy
- Calling 64-bit assembly language functions lodged inside the Delphi source code
- 大型网站架构系列:分布式消息队列(一) (转)
- Android平台下hook框架adbi的研究(下)
- Parcelable用法。
- eclipse代码自动补全
- MS WORD 通配符查找 正则表达式
- js中基本常识
- Spring IOC 方式结合TESTGN测试用例,测试简单java的命令模式
- Most Powerful (zoj 3471 状压dp 点集配对)
- Groovy在Spring中的简单使用
- 网络服务器开发总结