hdu5171(矩阵快速幂)
2024-09-20 23:48:41
题意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b(a,b∈S),现在GTY想最大化可重集的和,这个工作就交给你了。 注:可重集是指可以包含多个相同元素的集合
分析:想要和最大,那么每次必定从集合里面拿出最大的两个出来相加,然后k次后面就类似斐波那契数列了。
由斐波那契数列公式知:Fn=Fn-1+Fn-2,求和公式有Sn=Sn-1+Fn.因此用这两个公式构造矩阵,进行矩阵快速幂。
Sn-1 | 1 1 0| Sn
Fn * | 0 1 1| =Fn+1
Fn-1 | 0 1 0| Fn
然后ans=(Sn+(sum-a[n-1]-a[n-2]))%mod(sum为原集合总和,然后减去最大的两个,加上k次后的序列之和(即斐波那契数列和))。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 10000007
#define inf 0x3f3f3f3f
#define N 40010
#define clr(a) (memset(a,0,sizeof(a)))
using namespace std;
struct matrix
{
LL m[][];
};
LL a[];
matrix mult(matrix a,matrix b)
{
matrix c;
memset(c.m,,sizeof(c.m));
for(int i=;i<;i++)
for(int j=;j<;j++)
{
if(a.m[i][j]==)continue;
for(int k=;k<;k++)
{
if(b.m[j][k]==)continue;
c.m[i][k]+=a.m[i][j]*b.m[j][k]%mod;
c.m[i][k]%=mod;
}
}
return c;
}
matrix quickmod(matrix a,int n)
{
matrix temp;
memset(temp.m,,sizeof(temp.m));
for(int i=;i<=;i++)temp.m[i][i]=;
while(n)
{
if(n&)temp=mult(temp,a);
a=mult(a,a);
n/=;
}
return temp;
}
int main()
{
LL n,k;
while(scanf("%I64d%I64d",&n,&k)>)
{
LL sum=;
for(int i=;i<n;i++)scanf("%I64d",&a[i]),sum+=a[i];
sort(a,a+n);
matrix ans;
ans.m[][]=;ans.m[][]=;ans.m[][]=;
ans.m[][]=;ans.m[][]=;ans.m[][]=;
ans.m[][]=;ans.m[][]=;ans.m[][]=;
ans=quickmod(ans,k+);
printf("%I64d\n",(1LL*ans.m[][]*a[n-]+1LL*ans.m[][]*a[n-]+1LL*ans.m[][]*a[n-]+sum-(a[n-]+a[n-]))%mod);
}
}
最新文章
- Ioc
- Lyft押重注于苹果编程语言Swift
- slice,substr和substring的区别
- SQL: See the TSQL underneath the sp_execute calls
- javascrit2.0完全参考手册(第二版) 第2章第4节 基本的数据类型
- jar包程序 读取properties文件
- 转:三十二、Java图形化界面设计——布局管理器之CardLayout(卡片布局)
- Anatomy of a Program in Memory
- C和Java中数组的定义
- tornado\ioloop.py单例
- dfs.replication 参数 动态修改
- Android studio 安装的安装一些问题
- hdu 5274 Dylans loves tree
- android 开发Handler源码剖析
- 使用JumpServer管理你的服务器
- Ubuntu 中文拼音输入法键入异常
- 解决Ubuntu中vi命令的编辑模式下不能正常使用方向键和退格键的问题
- 优质产品需求文档(PRD)写作三大原则
- [Objective-C语言教程]继承(25)
- Git在Githib和Github上的使用
热门文章
- Buenos Aires和Rio de Janeiro怎么发音?
- ubuntu无法解析主机错误与解决的方法
- 执行shell脚本提示“syntax error near unexpected token for((i=0;i&;lt;$length;i++))”
- BLE简介和Android BLE编程
- 体验魅力Cognos BI 10 系列,第1 部分: 第一次安装
- Python源码学习十一 一个常用的内存分配函数
- C++ template error: undefined reference to XXX
- Sencha app build 出现 missing name after . operator 问题
- C++ 观察者模式样例
- Android开源框架AsyncHttpClient (android-async-http)使用