题目链接: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. Codeforces 691E Xor-sequences

    矩阵快速幂.递推式:dp[k][i]=sum(dp[k-1][j]*f[i][j]),dp[k][i]表示的意义是序列中有k个元素,最后一个元素是i的方案数,f[i][j]=1表示i与j能放在一起,反 ...

  2. luogu P2894 [USACO08FEB]酒店Hotel

    题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  3. spring beans 接口

  4. Ubuntu 16.04中的Dock的应用顺序调整

    操作步骤: 参考: https://askubuntu.com/questions/39805/is-there-an-easy-way-to-rearrange-or-move-the-icons- ...

  5. Oracle hidden costs revealed, Part2 – Using DTrace to find why writes in SYSTEM tablespace are slower than in others

    http://blog.tanelpoder.com/2008/09/02/oracle-hidden-costs-revealed-part2-using-dtrace-to-find-why-wr ...

  6. 获取path的几种方式:NSFileManager NSHomeDirectory NSBundle

    //---------------------------------------------------------------------------------NSFileManager *fi ...

  7. php命令行查看扩展信息

    通常,在php的开发过程中,我们会使用到第三方扩展,这时候,我们对于php扩展的信息的查看就显得尤为重要了.一般情况下,我们查看到扩展信息,都是直接通过 cat *.ini  文件来进行,这样子的效率 ...

  8. 3.环境搭建-Hadoop(CDH)集群搭建

    目录 目录 实验环境 安装 Hadoop 配置文件 在另外两台虚拟机上搭建hadoop 启动hdfs集群 启动yarn集群 本文主要是在上节CentOS集群基础上搭建Hadoop集群. 实验环境 Ha ...

  9. 将可执行程序的内存空间扩展到3GB(windows)

    为了告知操作系统这个应用程序可以支持/3GB方式,我们需要往exe 文件头中添加一个 IMAGE_FILE_LARGE_ADDRESS_AWARE 标志.添加的方式很简单: 在你的系统的 Progra ...

  10. Cocos2d-x学习笔记(18)(TestCpp源代码分析-2)

    本章主要讲controller.h/cpp文件的分析,该文件主要用于演示样例场景管理类TestController,用于显示全部演示样例的菜单. //controller.cpp #include & ...