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可以用矩阵快速幂
  • 构造矩阵如图:

\[\left[
\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 + 矩阵快速幂的更多相关文章

  1. Educational Codeforces Round 14E. Xor-sequences(矩阵快速幂)

    传送门 题意 给定序列,从序列中选择k(1≤k≤1e18)个数(可以重复选择),使得得到的排列满足\(x_i与x_{i+1}\)异或的二进制表示中1的个数是3的倍数.问长度为k的满足条件的序列有多少种 ...

  2. Educational Codeforces Round 52E(构造,快速幂)

    #include <bits/stdc++.h>using namespace std;const int mod=998244353;long long b[200007];long l ...

  3. 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 ...

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

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

  5. 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 ...

  6. codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)

    题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...

  7. Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】

    题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...

  8. 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂

    [题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...

  9. 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 ...

随机推荐

  1. f5售后查询

    登录: https://secure.f5.com/validate/validate.jsp http://boochem.blog.51cto.com/628505/633907

  2. 【校招面试 之 C/C++】第32题 C++ 11新特性(三)之for关键字

    1.for循环的一般写法: int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i = 0; i < 10; i++) cout ...

  3. day 17 re模块

    RE模块 import re 对一个大篇幅的字符串,按照你的规则找出想要的字符串 # 单个字符匹配 import re # \w 与 \W #字母数字下划线, 非 # print(re.findall ...

  4. go语言net包udp socket的使用

    udp与tcp的不同在于客户端请求方式不同,udp缺少Accept函数. 一个简单的udp客户端: package main; import ( "net" "log&q ...

  5. 在BCH硬分叉后防止重放攻击-1

    导致新加密货币的硬分叉为加密交换运营商带来了独特的机遇和挑战.Poloniex在最近的Bitcoin Cash硬叉之后面临的一个挑战是保护我们的客户免受重播攻击.由于SV链后面的开发团队选择在分叉后近 ...

  6. [z]nativeSql

    http://blog.csdn.net/chenallen1025/article/details/9169543 EntityManager API 提供了创建 Query 实例以执行原生 SQL ...

  7. devexpress之barManager 使用

    这次我不想使用ribboncontrol 控件 作为窗口菜单栏,也不想用传统的那种字体的方式 标题栏 一.Bars 1.   把BarManager组件添加到窗体中后,会自动创建两个空的 bars: ...

  8. hdu 5693 && LightOj 1422 区间DP

    hdu 5693 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5693 等差数列当划分细了后只用比较2个或者3个数就可以了,因为大于3的数都可以由2和3 ...

  9. vim窗口切换

    参考资料: http://www.cnblogs.com/litifeng/p/8282479.html 当用vim写代码的时候,我喜欢一边看着头文件中结构的定义,一边编写实现的代码,这样就经常用到多 ...

  10. djiango控制语句

    {# 从0开始的索引#} {% for foo in value %} {# 从0开始的索引#} <p>{{ forloop.counter0 }}: {{ foo }}</p> ...