hdu 5171 GTY's birthday gift(数学,矩阵快速幂)
题意:
开始时集合中有n个数。
现在要进行k次操作。
每次操作:从集合中挑最大的两个数a,b进行相加,得到的数添加进集合中。
以此反复k次。
问最后集合中所有数的和是多少。
(2≤n≤100000,1≤k≤1000000000)
思路:
写出来发现是要求Fibonaci的前n个数的和。
Fibonaci是用矩阵快速幂求的,这个也可以。
[Sn,Fn,Fn-1]=[某个矩阵]*[Sn-1,Fn-1,Fn-2]
[S2,F2,F1]=[2,1,1]
然后写,,,
这个代码有些繁琐,应该把矩阵操作单独写成函数。
日后更新。
代码:
- ll const mol = 10000007;
- int n,k;
- ll ans;
- ll matrix[4][4];
- int a[100005];
- void matrixSolve(int k){ // ¾ØÕóµÄk´Î·½
- if(k==0){
- mem(matrix,0);
- matrix[1][1]=1;
- matrix[2][2]=1;
- matrix[3][3]=1;
- return;
- }
- if(k==1){
- mem(matrix,0);
- matrix[1][1]=matrix[1][2]=matrix[1][3]=1;
- matrix[2][2]=matrix[2][3]=1;
- matrix[3][2]=1;
- return;
- }
- matrixSolve(k/2);
- ll tempMatrix[4][4];
- mem(tempMatrix,0);
- rep(i,1,3){
- rep(j,1,3){
- rep(k,1,3){
- tempMatrix[i][j]=(tempMatrix[i][j]+matrix[i][k]*matrix[k][j]%mol)%mol;
- }
- }
- }
- rep(i,1,3){
- rep(j,1,3){
- matrix[i][j]=tempMatrix[i][j];
- }
- }
- if((k&1)==1){
- ll temp2Matrix[4][4];
- mem(temp2Matrix,0);
- temp2Matrix[1][1]=temp2Matrix[1][2]=temp2Matrix[1][3]=1;
- temp2Matrix[2][2]=temp2Matrix[2][3]=1;
- temp2Matrix[3][2]=1;
- ll temp3Matrix[4][4];
- mem(temp3Matrix,0);
- rep(i,1,3){
- rep(j,1,3){
- rep(k,1,3){
- temp3Matrix[i][j]=(temp3Matrix[i][j]+tempMatrix[i][k]*temp2Matrix[k][j])%mol;
- }
- }
- }
- rep(i,1,3){
- rep(j,1,3){
- matrix[i][j]=temp3Matrix[i][j];
- }
- }
- }
- }
- ll solve(int k){ //calc Sk
- ll s2=2,f2=1,f1=1;
- matrixSolve(k-2);
- ll sk=(matrix[1][1]*s2+matrix[1][2]*f2+matrix[1][3]*f1)%mol;
- return sk;
- }
- int main(){
- while(cin>>n>>k){
- ans=0;
- rep(i,1,n){
- scanf("%d",&a[i]);
- ans=(ans+a[i])%mol;
- }
- sort(a+1,a+1+n);
- if(k==1){
- ans=(ans+a[n-1])%mol;
- ans=(ans+a[n])%mol;
- printf("%I64d\n",ans);
- }
- else{
- int xx=a[n-1];
- int yy=a[n];
- ll ans1=(solve(k)*(ll)xx)%mol;
- ll ans2=((solve(k+1)-1)*(ll)yy)%mol;
- ans=(ans+ans1+ans2)%mol;
- printf("%I64d\n",ans);
- }
- }
- return 0;
- }
hdu 5171 GTY's birthday gift(数学,矩阵快速幂)的更多相关文章
- GTY's birthday gift【矩阵快速幂】
题目大意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b ...
- HDU 5171 GTY's birthday gift 矩阵快速幂
GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu 5171 GTY's birthday gift
GTY's birthday gift 问题描述 GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次 ...
- BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)
GTY's math problem Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 1757 A Simple Math Problem(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- (hdu 6030) Happy Necklace 找规律+矩阵快速幂
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- hdu 4291 2012成都赛区网络赛 矩阵快速幂 ***
分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节 求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也 ...
随机推荐
- POJ1861 Network (Kruskal算法 +并查集)
Network Description Andrew is working as system administrator and is planning to establish a new net ...
- 2. Go并发编程--GMP调度
目录 1. 前言 1.1 Goroutine 调度器的 GMP 模型的设计思想 1.2 GMP 模型 1.3. 有关M和P的个数问题 1.4 P 和 M 何时会被创建 2. 调度器的设计策略 3. g ...
- PHP的变量赋值
这个标题估计很多人会不屑一顾,变量赋值?excuse me?我们学开发的第一课就会了好不好.但是,就是这样基础的东西,反而会让很多人蒙圈,比如,值和引用的关系.今天,我们就来具体讲讲. 首先,定义变量 ...
- 启动docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
启动docker提示: docker: Got permission denied while trying to connect to the Docker daemon socket at uni ...
- git 报错 gitThere is no tracking information for the current branch. Please specify which branch you w
新建本地分支后将本地分支推送到远程库, 使用git pull 或者 git push 的时候报错gitThere is no tracking information for the current ...
- mybatis关系表
<select id="selectSingleQuestion" resultType="remarkPaper"> select FrontTi ...
- 监控linux服务器工具nmon的使用
做压测时,需要查看服务器中的cpu.内存变化,但由于服务器是linux环境,则需要监控linux服务器的工具,下面用到的工具是nmon. 1.安装nmon.在网上下载nmon安装包,在linux服务器 ...
- ios web 媒体查询兼容
原文:https://blog.csdn.net/dear_zx/article/details/82785250 防止链接丢失,复制一下 兼容iphone4/4s: @media (device-h ...
- 鸿蒙内核源码分析(任务切换篇) | 看汇编如何切换任务 | 百篇博客分析OpenHarmony源码 | v41.03
百篇博客系列篇.本篇为: v41.xx 鸿蒙内核源码分析(任务切换篇) | 看汇编如何切换任务 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调度谁 ...
- 实现js读取Excel数据
如何通过js去读取excel中的数据 <!DOCTYPE html> <html lang="en"> <head> <meta char ...