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

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
ll n,m;
struct mat
{
ll a[105][105];
};
mat mat_mul(mat x,mat y)
{
mat res;
memset(res.a,0,sizeof(res.a));
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
for(int k=0;k<m;k++)
res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%MOD;
return res;
}
void mat_pow(ll n)
{
mat c,res;
c.a[0][0]=c.a[0][m-1]=1;
for(int i=1;i<m;i++)
{
c.a[i][i-1]=1;
} memset(res.a,0,sizeof(res.a));
for(int i=0;i<m;i++) res.a[i][i]=1;
n--;
while(n)
{
if(n&1) res=mat_mul(res,c);
c=mat_mul(c,c);
n=n>>1;
}
c=res;
ll ans=c.a[m-1][0]*2%MOD;
for(int j=1;j<m;j++)ans=(ans+c.a[m-1][j])%MOD;
cout<<ans<<endl;
}
int main()
{
cin>>n>>m;
mat_pow(n);
return 0;
}

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. 2019牛客暑期多校训练营(第四场)A meeting(dfs或dp,dp待更新)

    示例1: 输入: 4 21 23 13 42 4 输出:2 说明: They can meet at place 1 or 3. 题意:从K个点到达不联通图某个点需要的最短时间,这个最短时间是这K个人 ...

  2. 十八、Nand Flash驱动和Nor Flash驱动

    在读者学习本章之前,最好了解Nand Flash读写过程和操作,可以参考:Nand Flash裸机操作. 一开始想在本章写eMMC框架和设备驱动,但是没有找到关于eMMC设备驱动具体写法,所以本章仍继 ...

  3. 怎样让ssh连接保持连接, 而不会因为没有操作而中断

    因为安全方面的考虑, ssh服务默认在一段时间内不操作会断开连接, 解决方法修改ssh的配置文件, 让ssh每隔一段时间就自动进行一次连接, 以达到保持连接的目的. 首先找到ssh配置文件的位置: f ...

  4. hadoop入门-centos7.2安装hadoop2.8

    1. 安装准备 (1)必须安装jdk: 因为hadoop是基于Java实现的,所有必须安装jdk 是JDK不是jre jdk1.7 jdk1.8 (2)系统位数 (3)创建专用用户 useradd h ...

  5. mysql建表常用命令

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RD ...

  6. flask打包安装文件

    如果在一台新的电脑需要运行项目的时候,这时候就需要将项目项目所用到的模块都导出来 依赖文件生成pip freeze > requirements.txt 执行该命令会在项目根目录下生成一个 re ...

  7. CentOS 7中调整默认开启终端数量

    #vim /etc/securetty #然后将不需要使用到的进行注释掉就可以了 就这么简单,在强大的百度上找了半天愣是没找到

  8. 记录一下JProfiler的使用

    刚入职实习,第四天了,昨晚老大安排我在公司机器上装个JProfiler看一情况. 然后网上都是什么跟tomcat一起使用的,所以折腾了很久才搞出来. 我这里没用什么服务器,因为公司用的是Play!框架 ...

  9. SSH与ansible 部署方法与核心模块简介

    SSH与ansible 部署方法 部署环境 管理服务器 172.16.1.61 NFS服务器 172.16.1.31 备份服务器 172.16.1.41 1检查SSH服务是否运行并开启服务 netst ...

  10. SpringBoot之多数据源动态切换数据源

    原文:https://www.jianshu.com/p/cac4759b2684 实现 1.建库建表 首先,我们在本地新建三个数据库名分别为master,slave1,slave2,我们的目前就是写 ...