易得递推式为f[i]=f[i-1]+f[i-M] 最终答案即为f[N]. 由于N很大,用矩阵快速幂求解。

code:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int MOD=1e9+7;
  5. ll n,m;
  6. struct mat
  7. {
  8. ll a[105][105];
  9. };
  10. mat mat_mul(mat x,mat y)
  11. {
  12. mat res;
  13. memset(res.a,0,sizeof(res.a));
  14. for(int i=0;i<m;i++)
  15. for(int j=0;j<m;j++)
  16. for(int k=0;k<m;k++)
  17. res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%MOD;
  18. return res;
  19. }
  20. void mat_pow(ll n)
  21. {
  22. mat c,res;
  23. c.a[0][0]=c.a[0][m-1]=1;
  24. for(int i=1;i<m;i++)
  25. {
  26. c.a[i][i-1]=1;
  27. }
  28. memset(res.a,0,sizeof(res.a));
  29. for(int i=0;i<m;i++) res.a[i][i]=1;
  30. n--;
  31. while(n)
  32. {
  33. if(n&1) res=mat_mul(res,c);
  34. c=mat_mul(c,c);
  35. n=n>>1;
  36. }
  37. c=res;
  38. ll ans=c.a[m-1][0]*2%MOD;
  39. for(int j=1;j<m;j++)ans=(ans+c.a[m-1][j])%MOD;
  40. cout<<ans<<endl;
  41. }
  42. int main()
  43. {
  44. cin>>n>>m;
  45. mat_pow(n);
  46. return 0;
  47. }

Educational Codeforces Round 60 D. 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. 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 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

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

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

  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. 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 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  9. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

随机推荐

  1. 嵌入式Linux学习笔记之第二阶段---文件I/O

    1.文件IO的四个函数 一些术语: 不带缓冲的I/O: 每个read和write都调用内核中的一个系统调用. 文件描述符: 一个非负整数,对内核而言,所以打开的文件都通过文件描述符引用. ①打开或创建 ...

  2. Linux安装Python3流程

    安装必要的依赖库文件 yum -y install zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline- ...

  3. C++中深拷贝与浅拷贝

    浅拷贝和深拷贝 在某些状况下,类内成员变量需要动态开辟堆内存,如果实行位拷贝,也就是把对象里的值完全复制给另一个对象,如A=B.这时,如果B中有一个成员变量指针已经申请了内存,那A中的那个成员变量也指 ...

  4. Java基础IO类之数据流

    DataInputStream: 数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本java数据类型.应用程序可以使用数据输出流 写入稍后由数据输入流读取的数据.DataInputStrea ...

  5. stm32f103re stop模式下降低功耗

    一端口设置1.将N.C的GPIO统一配置为GPIO_Mode_AIN: 2.检查输入引脚,按照不工作时候的状态相应配置为  IPD/IPU,即避免在内部上/下拉电阻上消耗电流,而且该电流理论值为VCC ...

  6. Map、FlatMap 和 Reduce

    Map 作用是生成一个新数组,遍历原数组,将每个元素拿出来做一些变换然后 append 到新的数组中. [1, 2, 3].map((v) => v + 1) // -> [2, 3, 4 ...

  7. Gogs + Drone 实现CI/CD(CD)

    前文已经实现CI部分,本文继续以Asp.Net Core实现CD部分. 创建gogs仓库 首先在gogs创建一个空项目drone-ci-demo,本地新建一个asp.net core项目,并且在与.c ...

  8. PLSQL登录的时候Warning提示:Using a filter for all users can lead to poor performance!

    转自: https://blog.csdn.net/athena2015/article/details/81811908

  9. py map reduce filter 总结

    array=[1,3,4,71,2] ret=[] for i in array: ret.append(i**2) print(ret) #如果我们有一万个列表,那么你只能把上面的逻辑定义成函数 d ...

  10. Android笔记(十三) Android中的基本组件——文本

    Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种 EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑 ...