题目链接: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. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)

    题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...

  2. Java Scanner类中next()和nextLine()方法的区别

    今天在练习中遇到了调用Scanner类中的nextLine()输入字符串自动跳过的问题,在博客上看了两篇解答,原来是nextLine()误认了前面next()输入时的Enter,但还是想了一会儿才弄清 ...

  3. 电影TS/TC/SCR/R5/BD/HD/HC版本意思收集(转)

    一.版本说明: 1.CAM(枪版) CAM通常是用数码摄像机从电影院盗录.有时会使用小三角架,但大多数时候不可能使用,所以摄像机会抖动.因此我们看到画面通常偏暗人物常常会失真,下方的 字幕时常会出现倾 ...

  4. 讯飞语音识别Android-Demo

    import java.io.UnsupportedEncodingException; import android.app.Activity; import android.os.Bundle; ...

  5. SilverLight:布局(3)StackPanel 对象

    ylbtech-SilverLight-Layout: 布局(3)StackPanel 对象 A, Nesting Layout Containers(内嵌布局容器) B, StackPanel(队列 ...

  6. vue doubleclick 鼠标双击事件

    Vue-dblclick事件(此外事件还有mouseover,mouseout,click,mousdown...): v-on:dblclick="函数" v-on:click/ ...

  7. Controller 层实现

    一.实验介绍 1.1 实验内容 本节课程主要利用 Spring MVC 框架实现 Controller 层以及一些辅助类的实现. 1.2 实验知识点 Spring MVC 框架 1.3 实验环境 JD ...

  8. 1.excel如何让一列的数都乘以固定值

     让B列等于A列乘以39.37 1.我们先选中B列中要编辑的单元: 2.再在编辑栏中输入公式:=A2*39.37   (PS:*号即表示是×号) 3.公式输入后,按下快捷键:CTRL+回车:记住一定要 ...

  9. Tomcat服务器改主页 & jeesite框架改首页

    Tomcat服务器改主页: 方法一: 把原来的 ROOT 目录清空: 发布你自己的项目到 ROOT 目录下: 发布程序 /webapps/ROOT/WEB-INF/web.xml 中需要有默认首页定义 ...

  10. javascript点滴积累

    1. javascript中的array, set, map 均为数据容器,使用iterable内置的forEach方法 var a = ['A', 'B', 'C'];a.forEach(funct ...