GTY's birthday gift(矩阵快速幂)

来源:互联网 发布:js动态创建表格 编辑:程序博客网 时间:2024/05/22 08:24



Link: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): 1182    Accepted Submission(s): 453


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,bS), 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 (2n100000,1k1000000000). The second line contains n elements ai (1ai100000)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
 

Source
BestCoder Round #29
 





编程思想:矩阵快速幂。

AC code:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<queue>#include<map>#define LL long long#define MAXN 1000010using namespace std;const int INF=0x3f3f3f3f;//----以下为矩阵快速幂模板-----// //const int mod=3;//模3,故这里改为3即可 int mod;const int NUM=12;//定义矩阵能表示的最大维数 int N;//N表示矩阵的维数,以下的矩阵加法、乘法、快速幂都是按N维矩阵运算的 struct Mat{//矩阵的类LL a[NUM][NUM];void init()//将其初始化为单位矩阵  {memset(a,0,sizeof(a));for(int i=0;i<NUM;i++){a[i][i]=1;}}};Mat add(Mat a,Mat b)//(a+b)%mod  矩阵加法  {Mat ans;for(int i=0;i<N;i++){for(int j=0;j<N;j++){ans.a[i][j]=(a.a[i][j]%mod)+(b.a[i][j]%mod);ans.a[i][j]%=mod;}}return ans;}Mat mul(Mat a,Mat b) //(a*b)%mod  矩阵乘法  {Mat ans;for(int i=0;i<N;i++){for(int j=0;j<N;j++){ans.a[i][j]=0;for(int k=0;k<N;k++){ans.a[i][j]=(ans.a[i][j]%mod)+(a.a[i][k]%mod)*(b.a[k][j]%mod);}ans.a[i][j]%=mod;}}return ans;}Mat power(Mat a,int num)//(a^n)%mod  矩阵快速幂 {Mat ans;ans.init();while(num){if(num&1){ans=mul(ans,a);}num>>=1;a=mul(a,a);}return ans;}Mat pow_sum(Mat a,int num)//(a+a^2+a^3....+a^n)%mod 矩阵的幂和{int m;Mat ans,pre;if(num==1)return a;m=num/2;pre=pow_sum(a,m);ans=add(pre,mul(pre,power(a,m)));if(num&1)ans=add(ans,power(a,num));return ans;}void output(Mat a)//输出矩阵 {for(int i=0;i<N;i++){for(int j=0;j<N;j++){printf("%lld%c",a.a[i][j],j==N-1?'\n':' ');}}}//----以上为矩阵快速幂模板-----// int a[100010];int main(){Mat A,B;mod=10000007;int n,k,i,sum,ans;while(scanf("%d%d",&n,&k)!=EOF){sum=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);sum=(sum+a[i])%mod;}sort(a+1,a+n+1);N=3;A.a[0][0]=A.a[0][1]=A.a[0][2]=A.a[1][1]=A.a[1][2]=A.a[2][1]=1;A.a[1][0]=A.a[2][0]=A.a[2][2]=0;A=power(A,k);ans=(A.a[0][0]*sum%mod+A.a[0][1]*a[n]%mod+A.a[0][2]*a[n-1]%mod)%mod;printf("%d\n",ans);}return 0;} 



0 0
原创粉丝点击