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,所以要用到矩阵快速幂优化
注意初始化
代码:
- #include<bits/stdc++.h>
- using namespace std;
- #define mod 1000000007
- typedef long long ll;
- #define MAX 105
- const int N=;//矩阵的大小
- int T;
- ll n,m;
- ll add(ll a,ll b)
- {
- a%=mod;
- b%=mod;
- return (a+b)%mod;
- }
- struct hh
- {
- ll ma[N][N];
- }a,res;
- hh multi(hh a,hh b)
- {
- hh tmp;
- memset(tmp.ma,,sizeof(tmp.ma));
- for(int i=;i<N;i++)
- for(int j=;j<N;j++)
- for(int k=;k<N;k++)
- {
- tmp.ma[i][j]=add(tmp.ma[i][j],a.ma[i][k]*b.ma[k][j]);
- }
- return tmp;
- }
- void fast_pow(hh a,long long k)
- {
- memset(res.ma,,sizeof(res.ma));
- for(int i=;i<N;i++)res.ma[i][i]=;
- while(k>)
- {
- if(k&) res=multi(res,a);
- a=multi(a,a);
- k>>=;
- }
- }
- int main()
- {
- while(~scanf("%lld%d",&n,&m))
- {
- for(int i=;i<=m;i++) a.ma[i][i-]=;
- a.ma[][]=a.ma[][m]=;
- fast_pow(a,n);
- printf("%lld\n",res.ma[][]);
- }
- return ;
- }
Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- Educational Codeforces Round 60 (Rated for Div. 2)
A. Best Subsegment 题意 找 连续区间的平均值 满足最大情况下的最长长度 思路:就是看有几个连续的最大值 #include<bits/stdc++.h> using n ...
- 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 ...
- 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() ...
- 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 ...
- Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String
题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...
- 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 ...
随机推荐
- MySQL数据库的自动备份与数据库被破坏后的恢复(3)
[2] 当数据库被修改后的恢复方法 数据库被修改,可能存在着多方面的原因,被入侵.以及相应程序存在Bug等等,这里不作详细介绍.这里将只介绍在数据库被修改后,如果恢复到被修改前状态的方法. 具体和上面 ...
- json对象之间的转化
json字符串转化为 1.使用JSON.parse()函数 使用eval()函数 2.json对象转化为json字符串 使用JSON.stringify()
- __new__与__init__的区别
__new__ : 控制对象的实例化过程 , 在__init__方法之前调用 __init__ : 对象实例化对象进行属性设置 class User: def __new__(cls, *args, ...
- 用TweenMax.js动画让数字动起来
html: <div class="val2">0</div> js: let val2 = document.getElementsByClassName ...
- java伪代码读后感
愚公,在这位名家身上,浓缩了项目的组织者,团队经理,编程人员,技术分析师等众多角色的优秀素质.原始需求的产生“惩山北之塞,出入之迁”项目沟通的基本方式“聚室而谋之”项目的目标“毕力平险,指通豫南,达于 ...
- 跳马(Knight Moves), ZOJ1091, POJ2243 x
跳马(Knight Moves), ZOJ1091, POJ2243 题目描述: 给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值. 输入描述: 输入文件 ...
- 11年Macbook Air 安装win10 经验
11年macbook air安装win10折腾了好久,安装成功经验记录下. 1.使用BootCamp v6.0.6133 2.启动时U盘里面有两个引导Window和EFI,选择Windows启动.EF ...
- xml json mongo
w wuser@ubuntu:~/apiamzpy$ sudo pip install xmljson
- DSP处理器和ARM处理器的区别以及各自应用在那些领域
由于工作经常接触到各种多核的处理器,如TI的达芬奇系列芯片拥有1个DSP核3个ARM核.那么DSP处理器和ARM处理器各自有什么区别,各自适合那些领域? DSP:digital signal proc ...
- 【HTML】<!DOCTYPE html>作用
1.定义: DOCTYPE标签是一种标准通用标记语言的文档类型声明,它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(DTD)来解析文档. <!DOCTYPE> 声明 ...