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. Android Studio 如何使用jni

    在project视图下,main文件夹下,创建jniLibs文件夹,然后把so文件放入即可:

  2. Qt之Windows开发移植问题汇总

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc90101auw6.html 在用Qt开发完成项目后,就需要将其打包并且移植在其他机器上,能在其他PC机上正常跑起来才 ...

  3. java 数组变量与数组对象

    数组是否必须初始化 对于这个问题,关键在于要弄清楚数组变量和数组对象的差别.数组变量是存放在栈内存中的,数组对象是存放在堆内存中的.数组变量只是一个引用变量,他能够指向实际的数组对象. 所谓的数组初始 ...

  4. Androidndk开发打包时我们应该如何注意平台的兼容(x86,arm,arm-v7a)

    很多朋友在开发Android JNI的的时候,会遇到findlibrary returned null的错误,因为某种原因,so没有打包到apk中.下面浅析下引起该错误的原因以及平台兼容性问题. 一. ...

  5. .net获取根目录的方法集合

    编写程序的时候,经常需要用的项目根目录.自己总结如下 .取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径 方法2. ...

  6. 自己开发的轻量级gif动画录制工具

    虽然网上已经有LICEcap.GifCam等gif录制工具,但我仍然觉得对于我个人使用还是不够方面,所以自己又写了一个,功能相对简洁一些.    Gif Recorder 支持全屏录制和区域录制,可自 ...

  7. MySQL与Oracle的区别

    1.语法上的区别 变量类型定义.IN  OUT的位置.变量定义的位置.游标的位置.异常的位置: 2.MySQL没有 return 关键字,采用leave label的方式结束循环或跳出存储 3.异常处 ...

  8. sed awk 小例

    实现数据库批量更新与回滚 create database awktest; use awktest create table user(    id int unsigned not null uni ...

  9. 【贪心】 poj 1032 和为n的若干数最大乘积

    给出n,把n分解为若干不相同数之和,使之乘积最大.贪心,Discuss里面的思路:把n分解为从2开始的连续整数,如果有多,则从高位开始依次加1.如26,我们得到2+3+4+5+6,此时还剩余6(26- ...

  10. php取随机数 explode劫取字符串 时间定义随机数

    php取随机数 <?phpfunction randomkeys($length){ $pattern='1234567890'; for($i=0;$i<$length;$i++) {  ...