GTY's birthday gift

问题描述
 GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b (a,b\in S)a+b(a,b∈S),现在GTY想最大化可重集的和,这个工作就交给你了。
注:可重集是指可以包含多个相同元素的集合
输入描述
多组数据(约3组),每组数据的第一行有两个数n,k(2 \leq n \leq 100000,1 \leq k \leq 1000000000)n,k(2≤n≤100000,1≤k≤1000000000) 表示初始元素数量和可使用的魔法数,第二行包含n个数a(1 \leq a_i \leq 100000)a(1≤a​i​​≤100000)表示初始时可重集的元素
输出描述
对于每组数据,模10000007输出可重集可能的最大和。
输入样例
3 2
3 6 2
输出样例
35

这道题的解题思路很简单,用矩阵快速幂实现一个斐波那契数列求和(初值改变)

难点在于矩阵的构造,首先列出三个要维护的值
|   sum     |
|  a[n-1]  |
|  a[n-2]  |
那么可以想到这个矩阵的下一个形式必然为
| sum+a[n-1]+a[n-2] |
| a[n-1]+a[n-2]         |
| a[n-1]                     |
由此可以得到需要构造的友矩阵为
| 1 1 1 |
| 0 1 1 |
| 0 1 0 |
有了这个友矩阵,就可以进行矩阵快速幂了,先求

| 1 1 1 |
| 0 1 1 |  的K次方,然后再乘以初始矩阵就可以得到sum
| 0 1 0 |
 
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn=;
const int Max=1e5+;
const int MOD=;
int a[Max];
struct matrix
{
int w;
LL m[maxn][maxn]; //注意矩阵也要用LL,不然会出现溢出
matrix(int ww):w(ww){memset(m,,sizeof(m));};
matrix(){}
};
matrix operator * (matrix a,matrix b)
{
matrix res();
LL x;
for(int i=;i<res.w;i++)
{
for(int j=;j<res.w;j++)
{
x=;
for(int k=;k<res.w;k++)
{
x=(x+(LL)a.m[i][k]*b.m[k][j])%MOD;
}
res.m[i][j]=x;
}
}
return res;
}
matrix fast_cover(int k)
{
matrix base();
base.m[][]=base.m[][]=base.m[][]=;
base.m[][]=base.m[][]=base.m[][]=;
base.m[][]=base.m[][]=base.m[][]=;
matrix s();
s.m[][]=s.m[][]=s.m[][]=;
while(k)
{
if(k&) s=s*base;
base=base*base;
k>>=;
}
return s;
}
int main()
{
int n,k;LL sum;
while(scanf("%d%d",&n,&k)!=EOF)
{
sum=;
for(int i=;i<n;i++) scanf("%d",&a[i]),sum+=a[i];
sort(a,a+n);
matrix ss=fast_cover(k);
LL ans=(ss.m[][]*sum+ss.m[][]*a[n-]+ss.m[][]*a[n-])%MOD;
printf("%I64d\n",ans);
}
return ;
}

hdu 5171 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. hdu 5171 GTY's birthday gift(数学,矩阵快速幂)

    题意: 开始时集合中有n个数. 现在要进行k次操作. 每次操作:从集合中挑最大的两个数a,b进行相加,得到的数添加进集合中. 以此反复k次. 问最后集合中所有数的和是多少. (2≤n≤100000,1 ...

  3. 1002 GTY's birthday gift

    GTY's birthday gift                                                                       Time Limit ...

  4. hdu 5171(矩阵快速幂,递推)

    GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  5. HDU5171 GTY's birthday gift —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5171 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)  ...

  6. hdu 5172 GTY's gay friends

    GTY's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...

  7. HDU 5172 GTY's gay friends 线段树

    GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. HDU 5172 GTY's gay friends 线段树+前缀和+全排列

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...

  9. HDU 5170 GTY's math problem 水题

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5170 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

随机推荐

  1. SQL SERVER:一条SQL语句插入多条记录等

    在学习排名第二的mySql过程中,发现它的插入语句可以这样写: use test; create table fruits( fid char(10) not null ,s_id int null ...

  2. ios3--UIView的常见方法

    // // ViewController.m // 07-UIView的常见方法 // #import "ViewController.h" @interface ViewCont ...

  3. E: Unable to lock the administration directory (/var/lib/dpkg/)

    如何修复 Ubuntu 中的“Unable to lock the administration directory (/var/lib/dpkg/)” 在 Ubuntu 或者它的衍生版如 Linux ...

  4. ALSA声卡驱动中的DAPM详解之一:kcontrol

    DAPM是Dynamic Audio Power Management的缩写,直译过来就是动态音频电源管理的意思,DAPM是为了使基于linux的移动设备上的音频子系统,在任何时候都工作在最小功耗状态 ...

  5. 简单动态规划——最长公共子序列&&最长回文子序列&&最长上升||下降子序列

    最长公共子序列,顾名思义当然是求两个字符串的最长公共子序列啦,当然,这只是一道非常菜的动规,所以直接附上代码: #include<iostream> #include<cstdio& ...

  6. 65. ExtJs获取文本框中值的几种方式

    转自:https://blog.csdn.net/qiu512300471/article/details/17415675/ 1.Html文本框    如:<input type=" ...

  7. thinkphp的model的where条件的两种形式

    thinkphp的model的where查询时有两种形式. $model->field('id')->where('customer_num is null or customer_num ...

  8. Gym - 101982A 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) A. Exam

    题面 题意:你和朋友一起做了n道判断题,现在你知道你们两的答案,也知道你朋友对了k个,问你至少对了几个 题解:假设你和朋友n个答案都一样,那你是不是也对了k个,假设你和朋友有1个答案不一样,是不是,你 ...

  9. Jquery 表单基础元素操作总结

    最近做前端比较多总结一些常用功能: radio 单选选中并且出发change事件: $(selector).find('input:radio[name=valuationMode]').filter ...

  10. scrapy 框架持久化存储

    1.基于终端的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表或字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. # 执行输出指定格式进行存储:将 ...