题目链接: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. SD 胡策 Round 1 T3 彩尾巴猹的二进制数

    发现一个区间[L,R]代表的2进制数是3的倍数,当且仅当从L开始的后缀二进制值 - 从R+1开始的后缀二进制值 是 3 的倍数 (具体证明因为太简单而被屏蔽). 于是我们就可以在每个点维护从它开始的后 ...

  2. iOS -- SKPhysicsWorld类

    SKPhysicsWorld类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit. ...

  3. GO --微服务框架(二) goa

    之前用过go语言的反射来做一些代码生成,参考这篇. 但是这种方式,入侵太强,需要执行对应的申明调用, 所以对GOA框架的自动生成非常感兴趣,于是仔细研究了一下,发现用的比较巧妙, 这里先卖个关子,先看 ...

  4. weblogic的几点配置

    2.在tomcat下写过滤器以后还有的地方需要手工转码<-->weglobic下也不用 eg:SubjectAction.java3.weblogic下anltr.jar有冲突,需要从外界 ...

  5. 简化动态MERGE的SQL计算

    MSSQL.ORACLE等数据库支持MERGE语句更新表.但表结构未知时,因为缺乏集合类数据.用存储过程获得表结构再动态拼出SQL很麻烦,代码会有几十行之多:相同原因,用Java等高级语言实现也不简单 ...

  6. VueJS数据绑定文本显示:{{message}}

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  7. Cadence SPB 16. 6 安装步骤

    1.首先下载Cadence Allegro SPB orCAD16. 6 安装包,单击我,下载之后运行其中的setup.exe,然后先安装第一项License Manager

  8. oracle [union.minus.intersect]

    union 两张表的相同字段的数据[记录类型和列数要一致],合并,并且去重 can replace with "in" (但是如果是两个不同的表而且没什么关联的话必须要union了 ...

  9. FastDFS的配置、部署与API使用解读(5)FastDFS配置详解之Tracker配置(转)

    本文是 tracker.conf 配置文件的详细介绍. 1 基本配置 disable #func:配置是否生效 #valu:true.false disable=false bind_addr #fu ...

  10. oracle sqlplus 常用操作

    命令 含义 / 运行 SQL 缓冲区 ? [关键词] 对关键词提供 SQL 帮助 @[@] [文件名] [参数列表] 通过指定的参数,运行指定的命令文件 ACC[EPT] 变量 [DEF[AULT] ...