CF1117D Magic Gems

  • 考虑 \(dp\) , \(f[i]\) 表示用 \(i\) 个单位空间的方案数,答案即为 \(f[n]\).
  • 对于一个位置,我们可以放 \(Magic\) 的,占 \(m\) 空间,也可以放 \(Normal\) 的,占 \(1\) 空间.
  • 转移方程即为 \(f[i]=f[i-1]+f[i-m]\) ,边界条件为 \(f[0]=f[1]=f[2]=\dots f[m-1]=1\).
  • 直接转移是 \(O(n)\) 的,无法通过,需要矩阵优化.

  • 时间复杂度为 \(O(m^3logn)\) ,可以通过本题.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline ll read()
{
ll x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
const int MAXM=110;
const int P=1e9+7;
inline int add(int a,int b)
{
return (a + b) % P;
}
inline int mul(int a,int b)
{
return 1LL * a * b % P;
}
ll n;
int m;
struct Matrix{
int a[MAXM][MAXM];
Matrix()
{
for(int i=1;i<=m;++i)
for(int j=1;j<=m;++j)
a[i][j]=0;
}
Matrix operator * (const Matrix &rhs) const
{
Matrix res;
for(int k=1;k<=m;++k)
for(int i=1;i<=m;++i) if(a[i][k])
for(int j=1;j<=m;++j) if(rhs.a[k][j])
res.a[i][j]=add(res.a[i][j],mul(a[i][k],rhs.a[k][j]));
return res;
}
};
Matrix fpow(Matrix x,ll b)
{
Matrix res;
for(int i=1;i<=m;++i)
res.a[i][i]=1;
while(b)
{
if(b&1)
res=res*x;
x=x*x;
b>>=1;
}
return res;
}
int main()
{
n=read(),m=read();
if(n<m)
return puts("1")&0;
Matrix st;//st_{m-1}
for(int i=1;i<=m;++i)
st.a[i][1]=1;
Matrix trans;
trans.a[1][1]=trans.a[1][m]=1;
for(int i=2;i<=m;++i)
trans.a[i][i-1]=1;
st=fpow(trans,1LL*(n-m+1))*st;
cout<<st.a[1][1];
return 0;
}

CF1117D 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. CoderForces-Round60D(1117) Magic Gems

    D. Magic Gems time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  3. [递推+矩阵快速幂]Codeforces 1117D - Magic Gems

    传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...

  4. D. Magic Gems(矩阵快速幂 || 无敌杜教)

    https://codeforces.com/contest/1117/problem/D 题解:有一些魔法宝石,魔法宝石可以分成m个普通宝石,每个宝石(包括魔法宝石)占用1个空间,让你求占用n个空间 ...

  5. Educational Codeforces Round 60 D. Magic Gems

    易得递推式为f[i]=f[i-1]+f[i-M] 最终答案即为f[N]. 由于N很大,用矩阵快速幂求解. code: #include<bits/stdc++.h> using names ...

  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. eduCF#60 D. Magic Gems /// 矩阵快速幂

    题目大意: 给定n m (1≤N≤1e18, 2≤M≤100) 一个魔法水晶可以分裂成连续的m个普通水晶 求用水晶放慢n个位置的方案modulo 1000000007 (1e9+7) input 4 ...

  8. CF集萃2

    CF1155D - Beautiful Array 题意:给你一个序列和x,你可以选择任意一个子串(可以为空)乘上x,使得得到的序列最大子串和最大.求这个最大值.30w,2s. 解:设fi,0/1/2 ...

  9. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

随机推荐

  1. SonarQube介绍及部署

    SonarQube介绍及部署 编写人:罗旭成 编写时间:2014-5-28 SonarQube简介 SonarQube是一个用于代码质量管理的开源平台(Java开发),用于管理源代码的质量,可以从七个 ...

  2. 比较好的Json 格式数据

    { "81040753986": [{ "order_info": { "unique_package_reference": " ...

  3. C++ substr 和 substring

    功能相似,但参数不同 substr(start,length); substring(start,end);

  4. linux bash shell中,单引号、 双引号,反引号(``)的区别及各种括号的区别

    一.单引号和双引号 首先,单引号和双引号,都是为了解决中间有空格的问题. 因为空格在Linux中时作为一个很典型的分隔符,比如string1=this is astring,这样执行就会报错.为了避免 ...

  5. C#/JAVA 程序员转GO/GOLANG程序员笔记大全(DAY 00)

    一.安装说明 https://studygolang.com/dl 二.环境变量 // 下载 *.msi 安装文件,部分环境变量默认配置好了. 其他配置如下描述 三.目录及项目层级关系 在系统环境变量 ...

  6. opencv图像处理

    #coding=utf-8 import cv2 import numpy as np img1 = cv2.imread("3.jpg") img2 = cv2.imread(& ...

  7. cocos对象池的使用

    enemy.js cc.Class({ extends: cc.Component, properties: { enemySpeed: 0, //设置加速度 }, //初始化当前节点的y坐标 ini ...

  8. Dubbo + Zookeeper 简单配置

    Dubbo + Zookeeper Zookeeper 下载及配置 下载到本机/usr/local目录 wget https://mirrors.tuna.tsinghua.edu.cn/apache ...

  9. yii2 联系我们发送邮件报错

    为什么会报错,因为国内的邮件服务商要求发送邮件的人和设置的smtp服务器账号要相同,因为联系我们的是用户,也就是发件人是用户,而不是我们配置的邮箱,所有出错. 这里我用了个取巧的办法,发件人改为自己, ...

  10. 20165202 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:贾海粟 学号:20165202 指导教师:娄嘉鹏 实验日期:2018年4月16日 实验时间:15:3 ...