题目传送门

题意:

一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7)

思路:

线性dp,dp[i]=dp[i]+dp[i-m];

由于n到1e18,所以要用到矩阵快速幂优化

注意初始化

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define mod 1000000007
  4. typedef long long ll;
  5. #define MAX 105
  6. const int N=;//矩阵的大小
  7. int T;
  8. ll n,m;
  9. ll add(ll a,ll b)
  10. {
  11. a%=mod;
  12. b%=mod;
  13. return (a+b)%mod;
  14. }
  15. struct hh
  16. {
  17. ll ma[N][N];
  18. }a,res;
  19. hh multi(hh a,hh b)
  20. {
  21. hh tmp;
  22. memset(tmp.ma,,sizeof(tmp.ma));
  23. for(int i=;i<N;i++)
  24. for(int j=;j<N;j++)
  25. for(int k=;k<N;k++)
  26. {
  27. tmp.ma[i][j]=add(tmp.ma[i][j],a.ma[i][k]*b.ma[k][j]);
  28. }
  29. return tmp;
  30. }
  31. void fast_pow(hh a,long long k)
  32. {
  33. memset(res.ma,,sizeof(res.ma));
  34. for(int i=;i<N;i++)res.ma[i][i]=;
  35. while(k>)
  36. {
  37. if(k&) res=multi(res,a);
  38. a=multi(a,a);
  39. k>>=;
  40. }
  41. }
  42. int main()
  43. {
  44. while(~scanf("%lld%d",&n,&m))
  45. {
  46. for(int i=;i<=m;i++) a.ma[i][i-]=;
  47. a.ma[][]=a.ma[][m]=;
  48. fast_pow(a,n);
  49. printf("%lld\n",res.ma[][]);
  50. }
  51. return ;
  52. }

Educational Codeforces Round 60 (Rated for Div. 2) 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 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. MySQL数据库的自动备份与数据库被破坏后的恢复(3)

    [2] 当数据库被修改后的恢复方法 数据库被修改,可能存在着多方面的原因,被入侵.以及相应程序存在Bug等等,这里不作详细介绍.这里将只介绍在数据库被修改后,如果恢复到被修改前状态的方法. 具体和上面 ...

  2. json对象之间的转化

    json字符串转化为 1.使用JSON.parse()函数 使用eval()函数 2.json对象转化为json字符串 使用JSON.stringify()

  3. __new__与__init__的区别

    __new__  : 控制对象的实例化过程 , 在__init__方法之前调用 __init__ : 对象实例化对象进行属性设置 class User: def __new__(cls, *args, ...

  4. 用TweenMax.js动画让数字动起来

    html: <div class="val2">0</div> js: let val2 = document.getElementsByClassName ...

  5. java伪代码读后感

    愚公,在这位名家身上,浓缩了项目的组织者,团队经理,编程人员,技术分析师等众多角色的优秀素质.原始需求的产生“惩山北之塞,出入之迁”项目沟通的基本方式“聚室而谋之”项目的目标“毕力平险,指通豫南,达于 ...

  6. 跳马(Knight Moves), ZOJ1091, POJ2243 x

    跳马(Knight Moves), ZOJ1091, POJ2243 题目描述: 给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值. 输入描述: 输入文件 ...

  7. 11年Macbook Air 安装win10 经验

    11年macbook air安装win10折腾了好久,安装成功经验记录下. 1.使用BootCamp v6.0.6133 2.启动时U盘里面有两个引导Window和EFI,选择Windows启动.EF ...

  8. xml json mongo

    w wuser@ubuntu:~/apiamzpy$ sudo pip install xmljson

  9. DSP处理器和ARM处理器的区别以及各自应用在那些领域

    由于工作经常接触到各种多核的处理器,如TI的达芬奇系列芯片拥有1个DSP核3个ARM核.那么DSP处理器和ARM处理器各自有什么区别,各自适合那些领域? DSP:digital signal proc ...

  10. 【HTML】<!DOCTYPE html>作用

    1.定义: DOCTYPE标签是一种标准通用标记语言的文档类型声明,它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(DTD)来解析文档. <!DOCTYPE> 声明 ...