题目链接:https://vjudge.net/problem/HDU-5171

GTY's birthday gift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1760    Accepted Submission(s): 685

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
 
Source
 
Recommend
hujie

题意:

已经存在一个大小为n的集合,现在可以任意从中找到两个数,把它们的和加入集合中,这样的操作执行k次,那么这个集合的总和最大可以是多少?

题解:

可以推出斐波那契数列,那么就用矩阵快速幂求前n项,以及前n项和。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#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-;
const int mod = ;
const int maxn = +; int n,k;
int a[maxn]; struct MAT
{
LL mat[][];
void init() {
rep(i,,) rep(j,,)
mat[i][j] = (i==j);
}
}; MAT mul(MAT x, MAT y)
{
MAT s;
ms(s.mat,);
rep(i,,) rep(j,,) rep(k,,)
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&) s = mul(s,x);
x = mul(x,x);
y >>= ;
}
return s;
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
rep(i,,n)
scanf("%lld",&a[i]); sort(a+,a++n);
LL ans = ;
rep(i,,n)
ans += a[i], ans %= mod; if(k==)
{
ans += (a[n-]+a[n])%mod, ans %= mod;
cout<<ans<<endl;
continue;
} MAT s;
ms(s.mat,);
s.mat[][] = s.mat[][] = s.mat[][] = ;
s.mat[][] = s.mat[][] = s.mat[][] = ;
s = qpow(s,k-); ans += (1LL*(*a[n-]+*a[n])*s.mat[][])%mod, ans %= mod;
ans += (1LL*(*a[n-]+*a[n])*s.mat[][])%mod, ans %= mod;
ans += (1LL*(*a[n-]+*a[n])*s.mat[][])%mod, ans %= mod;
cout<<ans<<endl;
}
}

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

  1. HDU 5171 GTY's birthday gift 矩阵快速幂

    GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  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. hdu5171(矩阵快速幂)

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

  5. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. BZOJ4547 Hdu5171 小奇的集合 【矩阵快速幂优化递推】

    BZOJ4547 Hdu5171 小奇的集合 Description 有一个大小为n的可重集S,小奇每次操作可以加入一个数a+b(a,b均属于S),求k次操作后它可获得的S的和的最大值.(数据保证这个 ...

  7. HDU5171 矩阵快速幂

    题目描述:http://acm.hdu.edu.cn/showproblem.php?pid=5171 算法: 可以先将数组a[]排序,然后序列 a1 , a2 , … , an 即为有序序列,则第一 ...

  8. HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)

    传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少 ...

  9. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

随机推荐

  1. HDU 2874 Connections between cities(LCA)

    题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...

  2. java poi excel 生成表格的工具封装

    效果如下: 代码如下: import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import ...

  3. 匿名块的四个类型(type rowtype record table)

    Oracle PL/SQL块 匿名块的四个类型 type rowtype record table ---- type (列类型)  %type类型是指声明变量的时候,参考某个表的某个列的类型---- ...

  4. JVM--------3

    一.为什么要使用类加载器?Java语言里,类加载都是在程序运行期间完成的,这种策略虽然会令类加载时稍微增加一些性能开销,但是会给java应用程序提供高度的灵活性.例如:1.编写一个面向接口的应用程序, ...

  5. HttpClient的Post请求数据

    最近在项目中需要添加Post请求数据,以前的Get请求是使用JDK自带的URLConnection.在项目组人员的推荐下,开始使用HttpClient. HttpClient简介: HttpClien ...

  6. SG函数学习总结

    有点散乱, 将就着看吧. 首先是博弈论的基础, 即 N 和 P 两种状态: N 为必胜状态, P 为必败状态. 对于N, P两种状态, 则有 1. 没有任何合法操作的状态, P; 2. 可以移动到P局 ...

  7. 编译lua

    http://www.lua.org/ 新建一个 static library 工程,把解压得到的目录下的src子目录中的所有.h和.c文件拷贝到新工程目录下. 工程中删除自动生成的 main.c 文 ...

  8. 【前台 ajax】前台ajax请求,地址正确,但是报错不进入后台

    前台ajax请求,地址正确,但是报错不进入后台 出现上述问题,可能的情况是 1.ajax用的post,而后台限定用get,或者所有的post请求都被拦截,所以不能正常进入并且报错403 @Reques ...

  9. Go -- pprof协程监控

    go中有pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装了一 ...

  10. 11gR2 RAC 独占模式replace votedisk遭遇PROC-26,restore ocr遭遇CRS-4000、PROT-35

    原文链接:http://blog.itpub.net/23135684/viewspace-748816/ 11gR2 RAC系统的存储数据全然丢失,全部节点的软件都安装在本地磁盘中.本地磁盘保留了O ...