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. Git 删除文件

    在Git中,删除也是一种修改的操作,我们验证一下,先在工作目录中添加一个新文件test.txt,并且提交: $ git statusOn branch masterUntracked files:  ...

  2. php数据排序---array_multisort

    PHP代码 <?php $ar1 = array(10, 100, 100, 0); $ar2 = array(1, 3, 2, 4); array_multisort($ar1, $ar2); ...

  3. jni开发中的常见错误

    * java.lang.UnsatisfiedLinkError: Native method not found: 本地方法没有找到 * 本地函数名写错 * 忘记加载.so文件 没有调用System ...

  4. php添加gd

    一 GD简介: php处理图形的扩展库,提供了一系列用来处理图片的API.如果开发过程中发现有页面验证码不能显示,则要考虑检查phpinfo(),是否支持GD库. 二 思路: 网上发现添加GD库的方法 ...

  5. linq中first() firstordefault() last() lastOrDefault() single() singleOrDeafult

    一.firstordefault() 和 first() class Program { static void Main(string[] args) { List<Emp> list ...

  6. 实现jsp页面显示用户登录信息,利用session保存。

    这是后台代码 这是jsp代码,上面是声明,下面是获得值.

  7. selection与range笔记

    selection对象代表当前激活选中区,通常是高亮的文本块 创建选中区: 1.拖拽文本 2.脚本创建 cerateRange() 获取selection对象 IE     document.sele ...

  8. 【A + B + C + D】 问题

    A + B + C + D Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. Toy Storage POJ 2398

    题目大意:和 TOY题意一样,但是需要对隔板从左到右进行排序,要求输出的是升序排列的含有i个玩具的方格数,以及i值. 题目思路:判断叉积,二分遍历 #include<iostream> # ...

  10. 函数求值一<找规律>

    函数求值 题意: 定义函数g(n)为n最大的奇数因子.求f(n)=g(1)+g(2)+g(3)+-+g(n).1<=n<=10^8; 思路: 首先明白暴力没法过.问题是如何求解,二分.知道 ...