CF1117D Magic Gems

  • 考虑 \(dp\) , \(f[i]\) 表示用 \(i\) 个单位空间的方案数,答案即为 \(f[n]\).
  • 对于一个位置,我们可以放 \(Magic\) 的,占 \(m\) 空间,也可以放 \(Normal\) 的,占 \(1\) 空间.
  • 转移方程即为 \(f[i]=f[i-1]+f[i-m]\) ,边界条件为 \(f[0]=f[1]=f[2]=\dots f[m-1]=1\).
  • 直接转移是 \(O(n)\) 的,无法通过,需要矩阵优化.

  • 时间复杂度为 \(O(m^3logn)\) ,可以通过本题.
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define mp make_pair
  5. #define pii pair<int,int>
  6. inline ll read()
  7. {
  8. ll x=0;
  9. bool pos=1;
  10. char ch=getchar();
  11. for(;!isdigit(ch);ch=getchar())
  12. if(ch=='-')
  13. pos=0;
  14. for(;isdigit(ch);ch=getchar())
  15. x=x*10+ch-'0';
  16. return pos?x:-x;
  17. }
  18. const int MAXM=110;
  19. const int P=1e9+7;
  20. inline int add(int a,int b)
  21. {
  22. return (a + b) % P;
  23. }
  24. inline int mul(int a,int b)
  25. {
  26. return 1LL * a * b % P;
  27. }
  28. ll n;
  29. int m;
  30. struct Matrix{
  31. int a[MAXM][MAXM];
  32. Matrix()
  33. {
  34. for(int i=1;i<=m;++i)
  35. for(int j=1;j<=m;++j)
  36. a[i][j]=0;
  37. }
  38. Matrix operator * (const Matrix &rhs) const
  39. {
  40. Matrix res;
  41. for(int k=1;k<=m;++k)
  42. for(int i=1;i<=m;++i) if(a[i][k])
  43. for(int j=1;j<=m;++j) if(rhs.a[k][j])
  44. res.a[i][j]=add(res.a[i][j],mul(a[i][k],rhs.a[k][j]));
  45. return res;
  46. }
  47. };
  48. Matrix fpow(Matrix x,ll b)
  49. {
  50. Matrix res;
  51. for(int i=1;i<=m;++i)
  52. res.a[i][i]=1;
  53. while(b)
  54. {
  55. if(b&1)
  56. res=res*x;
  57. x=x*x;
  58. b>>=1;
  59. }
  60. return res;
  61. }
  62. int main()
  63. {
  64. n=read(),m=read();
  65. if(n<m)
  66. return puts("1")&0;
  67. Matrix st;//st_{m-1}
  68. for(int i=1;i<=m;++i)
  69. st.a[i][1]=1;
  70. Matrix trans;
  71. trans.a[1][1]=trans.a[1][m]=1;
  72. for(int i=2;i<=m;++i)
  73. trans.a[i][i-1]=1;
  74. st=fpow(trans,1LL*(n-m+1))*st;
  75. cout<<st.a[1][1];
  76. return 0;
  77. }

CF1117D Magic Gems的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  2. CoderForces-Round60D(1117) Magic Gems

    D. Magic Gems time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  3. [递推+矩阵快速幂]Codeforces 1117D - Magic Gems

    传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...

  4. D. Magic Gems(矩阵快速幂 || 无敌杜教)

    https://codeforces.com/contest/1117/problem/D 题解:有一些魔法宝石,魔法宝石可以分成m个普通宝石,每个宝石(包括魔法宝石)占用1个空间,让你求占用n个空间 ...

  5. Educational Codeforces Round 60 D. Magic Gems

    易得递推式为f[i]=f[i-1]+f[i-M] 最终答案即为f[N]. 由于N很大,用矩阵快速幂求解. code: #include<bits/stdc++.h> using names ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)

    题目传送门 题意: 一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7) 思路: 线性dp,dp[i]=dp[i]+dp[i-m]; 由于n到1e18,所以要用到矩阵快速幂优化 ...

  7. eduCF#60 D. Magic Gems /// 矩阵快速幂

    题目大意: 给定n m (1≤N≤1e18, 2≤M≤100) 一个魔法水晶可以分裂成连续的m个普通水晶 求用水晶放慢n个位置的方案modulo 1000000007 (1e9+7) input 4 ...

  8. CF集萃2

    CF1155D - Beautiful Array 题意:给你一个序列和x,你可以选择任意一个子串(可以为空)乘上x,使得得到的序列最大子串和最大.求这个最大值.30w,2s. 解:设fi,0/1/2 ...

  9. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

随机推荐

  1. 解决 对路径bin\roslyn..的访问被拒绝

    使用visual studio开发,一重新编译就会报错: 对路径“bin\roslyn\System.Reflection.Metadata.dll”的访问被拒绝 一开始的解决办法就是把bin下的文件 ...

  2. CDN专业一站式解决方案

    调度,弱网加速,动态防御,无限节点(重)新技术

  3. mina-deploy(3800🌟) 快速部署工具

    Mina  (3800

  4. RocketMQ学习分享

    消息队列的流派 什么是 MQ Message Queue(MQ),消息队列中间件.很多人都说:MQ 通过将消息的发送和接收分离来实现应用程序的异步和解偶,这个给人的直觉是——MQ 是异步的,用来解耦的 ...

  5. Java连接MySQL数据库——代码

    工具:eclipse MySQL5.7.17 MySQL连接驱动:mysql-connector-java-5.1.43.jar 加载驱动:我是用MAVEN进行管理 数据库连接信息: 数据库名称:wu ...

  6. HDU 5651 组合+逆元

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5651 题目意思我看了半天没读懂,一直以为是回文子串又没看见substring的单词最后看博客才知道是用给 ...

  7. Ansible 开发调试 之【模块调试】

    本地调试 需要安装jinja2 库 yum -y install python-jinja2 使用官方提供的测试脚本调试 git clone git://github.com/ansible/ansi ...

  8. Linux下利用Ret2Libc绕过DEP

    Linux下利用Ret2Libc绕过DEP ⑴.  原理分析: 系统库函数通常是不受DEP(关于DEP,可以查看我之前文章的详细介绍)保护的,所以通过将返回地址指向系统函数可以绕过DEP保护,所以可以 ...

  9. JQuery iframe

    子页面获取父页面的元素 function colisetapTJ() { var tapid = $('div:contains("添加档案报送"):last', window.p ...

  10. 启动和停止Oracle服务bat脚本

    总所周知,Oracle随开机启动会占很大内存,而你每次想用的时候还得去计算机服务里去找服务.一个一个的启动,比较麻烦. 这里给出两个bat脚本,来直接双击启动和停止Oracle服务[脚本内容来源于网络 ...