Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems

Time Limit: 3000 mSec

Problem Description

Input

The input contains a single line consisting of 2 integers N and M (1≤N≤10^18, 2≤M≤100).

Output

Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is N units. Print the answer modulo 1000000007.

Sample Input

4 2

Sample Output

5

题解:很简单的dp,考虑第一个数分裂不分裂,dp[n] = dp[n - m] + dp[n - 1],其中dp[n]就是站n个单位空间的方法数,由于N比较大,所以很明显要用矩阵快速幂,写这篇题解同时提醒自己快速幂之前一定要判断指数是否非负。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define REP(i, n) for (int i = 1; i <= (n); i++)
  6. #define sqr(x) ((x) * (x))
  7.  
  8. const int maxn = + ;
  9. const int maxm = + ;
  10. const int maxs = + ;
  11.  
  12. typedef long long LL;
  13. typedef pair<int, int> pii;
  14. typedef pair<double, double> pdd;
  15.  
  16. const LL unit = 1LL;
  17. const int INF = 0x3f3f3f3f;
  18. const double eps = 1e-;
  19. const double inf = 1e15;
  20. const double pi = acos(-1.0);
  21. const int SIZE = + ;
  22. const LL MOD = ;
  23.  
  24. struct Matrix
  25. {
  26. int r, c;
  27. LL mat[SIZE][SIZE];
  28. Matrix() {}
  29. Matrix(int _r, int _c)
  30. {
  31. r = _r;
  32. c = _c;
  33. memset(mat, , sizeof(mat));
  34. }
  35. };
  36.  
  37. Matrix operator*(Matrix a, Matrix b)
  38. {
  39. Matrix s(a.r, b.c);
  40. for (int i = ; i < a.r; i++)
  41. {
  42. for (int k = ; k < a.c; k++)
  43. { //j, k交换顺序运行更快
  44. for (int j = ; j < b.c; j++)
  45. {
  46. s.mat[i][j] = (s.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
  47. }
  48. }
  49. }
  50. return s;
  51. }
  52.  
  53. Matrix pow_mod(Matrix a, LL n)
  54. {
  55. Matrix ret(a.r, a.c);
  56. for (int i = ; i < a.r; i++)
  57. {
  58. ret.mat[i][i] = ; //单位矩阵
  59. }
  60. Matrix tmp(a);
  61. while (n)
  62. {
  63. if (n & )
  64. ret = ret * tmp;
  65. tmp = tmp * tmp;
  66. n >>= ;
  67. }
  68. return ret;
  69. }
  70.  
  71. LL n, m;
  72.  
  73. int main()
  74. {
  75. ios::sync_with_stdio(false);
  76. cin.tie();
  77. //freopen("input.txt", "r", stdin);
  78. //freopen("output.txt", "w", stdout);
  79. cin >> n >> m;
  80. if (n < m)
  81. {
  82. cout << << endl;
  83. return ;
  84. }
  85. Matrix ori = Matrix(m, );
  86. for (int i = ; i < m; i++)
  87. {
  88. ori.mat[i][] = unit;
  89. }
  90. Matrix A = Matrix(m, m);
  91. A.mat[][] = unit;
  92. A.mat[][m - ] = unit;
  93. for (int i = ; i < m; i++)
  94. {
  95. A.mat[i][i - ] = unit;
  96. }
  97. Matrix ans = pow_mod(A, n - m + );
  98. ori = ans * ori;
  99. cout << ori.mat[][] << endl;
  100. return ;
  101. }

Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)的更多相关文章

  1. 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,所以要用到矩阵快速幂优化 ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  3. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

  4. Educational Codeforces Round 60 (Rated for Div. 2)

    A. Best Subsegment 题意 找 连续区间的平均值  满足最大情况下的最长长度 思路:就是看有几个连续的最大值 #include<bits/stdc++.h> using n ...

  5. Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)

    #include <bits/stdc++.h>using namespace std;const long long mod = 1e9+7;unordered_map<long ...

  6. Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)

    #include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size() ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship

    time limit per test 2 second memory limit per test 256 megabytes input standard inputoutput standard ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String

    题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...

  9. Educational Codeforces Round 76 (Rated for Div. 2) B. Magic Stick 水题

    B. Magic Stick Recently Petya walked in the forest and found a magic stick. Since Petya really likes ...

随机推荐

  1. springboot 多线程执行

    一.springboot开线程执行异步任务 1.Spring通过任务执行器TaskExecutor,来实现多线程和并发编程,使用ThreadPoolTaskExecutor可实现一个基于线程池的Tas ...

  2. H5直播避坑指南

    本文来自"小时光茶社(Tech Teahouse)"公众号 作者简介: 文赫,2015年加入腾讯,作为前端开发工程师参与过手Q游戏公会,游戏中心,企鹅电竞等项目,具有丰富的移动端开 ...

  3. [Inside HotSpot] C1编译器工作流程及中间表示

    1. C1编译器线程 C1编译器(aka Client Compiler)的代码位于hotspot\share\c1.C1编译线程(C1 CompilerThread)会阻塞在任务队列,当发现队列有编 ...

  4. chrome谷歌浏览器开发者工具-网络带宽控制

    有时候我们想在本地测试一下图片预加载loading的加载情况,如下图: 可无奈一般网络带宽都比较好,基本上看不到效果,图片一下子就加载出来了, 可能这个时候有些小伙伴想到的办法是用定时器延迟加载 其实 ...

  5. mac电脑 上强大的RAW图像处理工具 ——RAW Power

    苹果电脑曾经有一款名为Aperture的照片处理应用,最终因为苹果软件策略的更好与升级,这款应用已经被苹果砍掉.但Aperture的开发者们并未放弃这款应用,在Mac OS上推出了一款名为RAW Po ...

  6. 【机器学习】--鲁棒性调优之L1正则,L2正则

    一.前述 鲁棒性调优就是让模型有更好的泛化能力和推广力. 二.具体原理 1.背景 第一个更好,因为当把测试集带入到这个模型里去.如果测试集本来是100,带入的时候变成101,则第二个模型结果偏差很大, ...

  7. WebApiClient的SteeltoeOSS.Discovery扩展

    1 背景 从园子里看到一些朋友在某些项目开发中,选择的架构是spring cloud搭建底层微服务框架,dotnet core来编写业务逻辑,SteeltoeOSS.Discovery是dotnet和 ...

  8. 图像检索(6):局部敏感哈希索引(LSH)

    图像检索中,对一幅图像编码后的向量的维度是很高.以VLAD为例,基于SIFT特征点,设视觉词汇表的大小为256,那么一幅图像编码后的VLAD向量的长度为$128 \times 256 = 32768 ...

  9. Django之随机图形验证码

    实现效果:点击右边图片验证码会变 前端代码: <div class="container"> <div class="row"> < ...

  10. (一) Keras 一元线性回归

    视频学习来源 https://www.bilibili.com/video/av40787141?from=search&seid=17003307842787199553 笔记 环境为 an ...