题意:

开始时集合中有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]

然后写,,,

这个代码有些繁琐,应该把矩阵操作单独写成函数。

日后更新。

代码:

  1. ll const mol = 10000007;
  2.  
  3. int n,k;
  4. ll ans;
  5. ll matrix[4][4];
  6. int a[100005];
  7.  
  8. void matrixSolve(int k){ // ¾ØÕóµÄk´Î·½
  9. if(k==0){
  10. mem(matrix,0);
  11. matrix[1][1]=1;
  12. matrix[2][2]=1;
  13. matrix[3][3]=1;
  14. return;
  15. }
  16. if(k==1){
  17. mem(matrix,0);
  18. matrix[1][1]=matrix[1][2]=matrix[1][3]=1;
  19. matrix[2][2]=matrix[2][3]=1;
  20. matrix[3][2]=1;
  21. return;
  22. }
  23. matrixSolve(k/2);
  24. ll tempMatrix[4][4];
  25. mem(tempMatrix,0);
  26. rep(i,1,3){
  27. rep(j,1,3){
  28. rep(k,1,3){
  29. tempMatrix[i][j]=(tempMatrix[i][j]+matrix[i][k]*matrix[k][j]%mol)%mol;
  30. }
  31. }
  32. }
  33. rep(i,1,3){
  34. rep(j,1,3){
  35. matrix[i][j]=tempMatrix[i][j];
  36. }
  37. }
  38. if((k&1)==1){
  39. ll temp2Matrix[4][4];
  40. mem(temp2Matrix,0);
  41. temp2Matrix[1][1]=temp2Matrix[1][2]=temp2Matrix[1][3]=1;
  42. temp2Matrix[2][2]=temp2Matrix[2][3]=1;
  43. temp2Matrix[3][2]=1;
  44.  
  45. ll temp3Matrix[4][4];
  46. mem(temp3Matrix,0);
  47. rep(i,1,3){
  48. rep(j,1,3){
  49. rep(k,1,3){
  50. temp3Matrix[i][j]=(temp3Matrix[i][j]+tempMatrix[i][k]*temp2Matrix[k][j])%mol;
  51. }
  52. }
  53. }
  54. rep(i,1,3){
  55. rep(j,1,3){
  56. matrix[i][j]=temp3Matrix[i][j];
  57. }
  58. }
  59. }
  60.  
  61. }
  62.  
  63. ll solve(int k){ //calc Sk
  64. ll s2=2,f2=1,f1=1;
  65. matrixSolve(k-2);
  66. ll sk=(matrix[1][1]*s2+matrix[1][2]*f2+matrix[1][3]*f1)%mol;
  67. return sk;
  68. }
  69.  
  70. int main(){
  71.  
  72. while(cin>>n>>k){
  73. ans=0;
  74. rep(i,1,n){
  75. scanf("%d",&a[i]);
  76. ans=(ans+a[i])%mol;
  77. }
  78. sort(a+1,a+1+n);
  79. if(k==1){
  80. ans=(ans+a[n-1])%mol;
  81. ans=(ans+a[n])%mol;
  82. printf("%I64d\n",ans);
  83. }
  84. else{
  85. int xx=a[n-1];
  86. int yy=a[n];
  87. ll ans1=(solve(k)*(ll)xx)%mol;
  88. ll ans2=((solve(k+1)-1)*(ll)yy)%mol;
  89. ans=(ans+ans1+ans2)%mol;
  90. printf("%I64d\n",ans);
  91. }
  92. }
  93.  
  94. return 0;
  95. }

hdu 5171 GTY's birthday gift(数学,矩阵快速幂)的更多相关文章

  1. GTY's birthday gift【矩阵快速幂】

    题目大意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b ...

  2. HDU 5171 GTY's birthday gift 矩阵快速幂

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

  3. hdu 5171 GTY's birthday gift

    GTY's birthday gift 问题描述 GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次 ...

  4. 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 ...

  5. 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) + …… ...

  6. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. (hdu 6030) Happy Necklace 找规律+矩阵快速幂

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...

  8. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

  9. 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也 ...

随机推荐

  1. POJ1861 Network (Kruskal算法 +并查集)

    Network Description Andrew is working as system administrator and is planning to establish a new net ...

  2. 2. Go并发编程--GMP调度

    目录 1. 前言 1.1 Goroutine 调度器的 GMP 模型的设计思想 1.2 GMP 模型 1.3. 有关M和P的个数问题 1.4 P 和 M 何时会被创建 2. 调度器的设计策略 3. g ...

  3. PHP的变量赋值

    这个标题估计很多人会不屑一顾,变量赋值?excuse me?我们学开发的第一课就会了好不好.但是,就是这样基础的东西,反而会让很多人蒙圈,比如,值和引用的关系.今天,我们就来具体讲讲. 首先,定义变量 ...

  4. 启动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 ...

  5. 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 ...

  6. mybatis关系表

    <select id="selectSingleQuestion" resultType="remarkPaper"> select FrontTi ...

  7. 监控linux服务器工具nmon的使用

    做压测时,需要查看服务器中的cpu.内存变化,但由于服务器是linux环境,则需要监控linux服务器的工具,下面用到的工具是nmon. 1.安装nmon.在网上下载nmon安装包,在linux服务器 ...

  8. ios web 媒体查询兼容

    原文:https://blog.csdn.net/dear_zx/article/details/82785250 防止链接丢失,复制一下 兼容iphone4/4s: @media (device-h ...

  9. 鸿蒙内核源码分析(任务切换篇) | 看汇编如何切换任务 | 百篇博客分析OpenHarmony源码 | v41.03

    百篇博客系列篇.本篇为: v41.xx 鸿蒙内核源码分析(任务切换篇) | 看汇编如何切换任务 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调度谁 ...

  10. 实现js读取Excel数据

    如何通过js去读取excel中的数据 <!DOCTYPE html> <html lang="en"> <head> <meta char ...