GTY's birthday gift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) 

【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】


【题意】

按照规则扩展一个集合k次,然后求其总和。

【分析】

扩展规则很简单,就是一个斐波那契数列,但是如果按照模拟的方法手动推算,复杂度对于本题的数据范围来说是不太合适的。
可以利用矩阵快速幂来迅速完成。
                    [1,,]
[S n-,F n,F n-] * [,,] =[S n,F n+,F n]
[,,]

剩下要注意的就是数据范围要开到long long了,因为可能涉及到10^9 * 10^9这样的数量级。

 /* ***********************************************
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[][];
} matrix; matrix ex=
{
,,,
,,,
,,
}; matrix mat(matrix a,matrix b)
{
matrix c;
for (int i=;i<;i++)
for (int j=;j<;j++)
{
c.m[i][j]=;
for (int k=;k<;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=;j<;j++)
{
c.m[][j]=;
for (int k=;k<;k++) c.m[][j]+=(a.m[][k]*b.m[k][j])%MOD;
c.m[][j]%=MOD;
}
return c;
} matrix doexpmat(matrix b,int n)
{
matrix a=
{
,,,
,,,
,,
};
while(n)
{
if (n&) a=mat(a,b);
n=n>>;
b=mat(b,b);
}
return a;
} int main()
{
int n,k;
int a[];
while(scanf("%d%d",&n,&k)==)
{
long long sum=;
for (int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum=(sum+a[i])%MOD;
}
sort(&a[],&a[n+]);
matrix start;
start.m[][]=;
start.m[][]=a[n];
start.m[][]=a[n-];
start=mat2(start,doexpmat(ex,k)); sum=(sum+start.m[][])%MOD;
printf("%lld\n",sum);
} return ;
}

HDU 5171 GTY's birthday gift 矩阵快速幂的更多相关文章

  1. HDU5171 GTY's birthday gift —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5171 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)  ...

  2. BC#29A:GTY's math problem(math) B:GTY's birthday gift(矩阵快速幂)

    A: HDU5170 这题让比较a^b与c^d的大小.1<=a,b,c,d<=1000. 显然这题没法直接做,要利用对数来求,但是在math库中有关的对数函数返回的都是浮点数,所以这又要涉 ...

  3. hdu 5171 GTY's birthday gift(数学,矩阵快速幂)

    题意: 开始时集合中有n个数. 现在要进行k次操作. 每次操作:从集合中挑最大的两个数a,b进行相加,得到的数添加进集合中. 以此反复k次. 问最后集合中所有数的和是多少. (2≤n≤100000,1 ...

  4. hdu 5171 GTY's birthday gift

    GTY's birthday gift 问题描述 GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次 ...

  5. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  6. HDU 5950:Recursive sequence(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...

  7. HDU 3292 【佩尔方程求解 && 矩阵快速幂】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...

  8. HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...

  9. hdu 4565 So Easy! (共轭构造+矩阵快速幂)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...

随机推荐

  1. CSU 1004并查集

    试题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1004 题目分析: 讲述的主要是是否可以通过公交直接到达自己的目的地,如果最后将问题转换为 ...

  2. spark第二篇--基本原理

    ==是什么 == 目标Scope(解决什么问题) 在大规模的特定数据集上的迭代运算或重复查询检索 官方定义 aMapReduce-like cluster computing framework de ...

  3. DLL调试方法

    1.已经做好的dll不能设置:你可以用AfxMessageBox把信息打印出来.2.哪个地方调用的函数 把DLL重新编译一次 在把DLL放到工程里 从新添加一下 然后在你工程调用DLL内容的地方设置断 ...

  4. Django:之传递数据给JS、Ajax和Ajax CSRF认证

    Django传递数据给JS 有时候我们想把一个list或者dict传递给javascript,处理后显示到网页上,比如要用js进行可视化到数据. 请注意:如果是不处理,直接显示在网页上,用Django ...

  5. java FLOAT

    System.out.println(""+ 1/2); 得不到0.5,只能得到0. 要想打印出浮点数,必须除数和被除数至少有一个是浮点数,像这样: System.out.prin ...

  6. docker 基础命令

    检查Docker安装是否正确docker info拉取镜像docker pull (image name)启动docker run -d -d 后台运行查看日志docker logs $sample_ ...

  7. Inno Setup入门(十九)——Inno Setup类参考(5)

    : Install Setup 2013-02-02 11:29 377人阅读 评论(0) 收藏 举报 单选按钮 单选按钮在安装中也很常见,例如同一个程序可以选择安装不同的性质的功能,例如选择32位或 ...

  8. 移植WL18XX到高通的时候,会出现几个.KO文件没有编译出来的情况

    1.检查kernel的.config文件,看是否有CONFIG又恢复了.这个时候就要需找依赖.把依赖使能 2.然后再去驱动源码检查 KCONFIG 的依赖,使能改使能的配置就可以了.

  9. iwlist等工具的移植

    http://blog.csdn.net/jk110333/article/details/8658054 参考了这边文章 -------------------------------------- ...

  10. 产生一个int数组,长度为100,并向其中随机插入1-100,不重复

    #define RANDOM(X) (rand() % X + 1) int main() { //标志数组 ] = {}; ] = {}; //默认的随机数种子是1,这样的话,每次执行这个程序都会得 ...