Educational Codeforces Round 60 D dp + 矩阵快速幂
https://codeforces.com/contest/1117/problem/D
题意
有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组成n颗宝石有多少种方法
题解
- 数据很大:找规律or矩阵快速幂
- 转移方程: dp[i]=dp[i-1]+dp[i-m]
- 因为n<=1e18可以用矩阵快速幂
- 构造矩阵如图:
\begin{matrix}
f[i-1] & f[i-2] & \cdots & f[i-m] \\
\end{matrix}
\right]
*
\left[
\begin{matrix}
1 & 1 &0 & \cdots & 0 \\
0 & 0 &1 & \cdots & 0 \\
\vdots & \vdots &\vdots &\ddots & \vdots \\
0 & 0 &0 &\cdots & 1 \\
1 & 0 &0 &\cdots & 0 \\
\end{matrix}
\right]
=
\left[
\begin{matrix}
f[i] & f[i-1] & \cdots & f[i-m+1] \\
\end{matrix}
\right]
\]
代码(矩阵快速幂板子)
#include<bits/stdc++.h>
#define P 1000000007
#define ll long long
#define M 105
using namespace std;
struct N{
ll a[M][M];
};
ll m,n,i,j;
N mul(N x,N y){
N z;
memset(z.a,0,sizeof(z.a));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++){
z.a[i][j]+=x.a[i][k]*y.a[k][j]%P;
z.a[i][j]%=P;
}
return z;
}
N pw(N bs,ll x){
N y;
memset(y.a,0,sizeof(y.a));
for(int i=1;i<=n;i++)y.a[i][i]=1;
while(x){
if(x&1)y=mul(y,bs);
bs=mul(bs,bs);
x>>=1;
}
return y;
}
int main(){
cin>>m>>n;
N f;memset(f.a,0,sizeof(f.a));
f.a[1][1]=1;f.a[n][1]=1;
for(i=1,j=2;j<=n;j++,i++)f.a[i][j]=1;
f=pw(f,m);
cout<<f.a[1][1]%P;
}
Educational Codeforces Round 60 D dp + 矩阵快速幂的更多相关文章
- Educational Codeforces Round 14E. Xor-sequences(矩阵快速幂)
传送门 题意 给定序列,从序列中选择k(1≤k≤1e18)个数(可以重复选择),使得得到的排列满足\(x_i与x_{i+1}\)异或的二进制表示中1的个数是3的倍数.问长度为k的满足条件的序列有多少种 ...
- Educational Codeforces Round 52E(构造,快速幂)
#include <bits/stdc++.h>using namespace std;const int mod=998244353;long long b[200007];long l ...
- 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) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- 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 ...
- codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)
题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...
- 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂
[题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
随机推荐
- vue 使用a+ router.push的形式跳转时,地址栏不显示参数
解决办法: a链接不要写href 属性
- DES对称加密
DES是对称性加密里面常见一种,全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对称性加密,加密 ...
- 6-完美解决Error:SSL peer shut down incorrectly
转载自: 完美解决Error:SSL peer shut down incorrectly 打开gradle文件夹下的gradle-wrapper文件 修改其中的配置文件将红色区域修改为http:// ...
- cherry-pick 命令
拣选会提取某次提交的补丁,之后尝试将其重新应用到当前分支上. 这种方式在你只想引入特性分支中的某个提交时很有用. 假设你的项目提交历史如下: 如果你希望将提交 e43a6 拉取到 master 分支, ...
- 队列 和 线程 之GCD dispatch
1.dispatch_queue_create 创建队列开启异步线程(1,4,2,3) // 创建一个队列 dispatch_queue_t queue = dispatch_queue_creat ...
- golang 创建一个简单的广播echo服务器
package main; import ( "net" "fmt" "bufio" ) //里面的代码部分参考cmu440课程 //htt ...
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- pom.xml中坐标的组成
坐标=组织(也就是所谓的公司名称)+项目名称+版本(如果不加范围默认为compile)
- Struts2框架之Action类的访问
1. 通过<action>标签中的method属性,访问到Action中的具体的方法. * 传统的配置方式,配置更清晰更好理解!但是扩展需要修改配置文件等! * 具体的实例如下: * 页面 ...
- [z] Git pull 强制覆盖本地文件
git fetch --all git reset --hard origin/master git pull git fetch 只是下载远程的库的内容,不做任何的合并 git reset 把HEA ...