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. 设置ulabel的行间距

    NSString *text = @"我是一个好人,12份绿色购物个 i 认为个人我国 i 加热哦围观 i我国3噢奇怪级我过街天桥哦推荐我她否认"; NSMutableParagr ...

  2. CentOS 7 时区设置

    设置时区同样, 在 CentOS 7 中, 引入了一个叫 timedatectl 的设置设置程序. 用法很简单: # timedatectl # 查看系统时间方面的各种状态 $timedatectl  ...

  3. c_select 调用参数说明

    c_select 调用 1. select系统调用select系统调用是用来让我们的程序监视多个文件描述符的状态变化的.程序会停在select这里等待,直到被监视的文件描述符有某一个或多个发生了状态改 ...

  4. Git中的merge命令实现中出现问题及其解决

    Git中的merge命令实现和工作方式 2015年8月17日星期一 丹丹 git代码在合并两个分支的时候总是会出现一下的错误提示,不能正常的完成合并分支,错误提示如图所示: 但是在其他的终端是可以完成 ...

  5. Flex4.6 DataGrid GridItemRenderer宣染器

    本文转自:http://blog.sina.com.cn/s/blog_71848dcf01012ctl.html,稍作修改 <?xml version="1.0" enco ...

  6. 字符串查找 cmd find命令

    find /i "ora-" *.log 我对findstr是如此的依赖,以至于当我向各位讲解find命令的时候,我还得老老实实地在cmd窗口中敲下 find /? 这条命令,然后 ...

  7. Anton and School

    Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. ibatis 自动生成map,bean,dao

    1.下载 AbatorForEclipse1.1.0 地址:http://download.csdn.net/detail/fym548/9426877 点击Archive按钮选择下载的,然后重启My ...

  9. .gitigore 相关

    为什么要配置.gitigore 在我们使用git的过程当中,不是任何文件都需要commit到本地或者远程仓库的,比如一些三方库文件.那么作为一个git新手,很多人不知道如何配置.gitignore文件 ...

  10. ListView的淡入淡出和Activity的淡入淡出补间动画效果Animation

    //=========主页面======================= package com.bw.lianxi7; import android.os.Bundle;import androi ...