Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems

Time Limit: 3000 mSec

Problem Description

Input

The input contains a single line consisting of 2 integers N and M (1≤N≤10^18, 2≤M≤100).

Output

Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is N units. Print the answer modulo 1000000007.

Sample Input

4 2

Sample Output

5

题解:很简单的dp,考虑第一个数分裂不分裂,dp[n] = dp[n - m] + dp[n - 1],其中dp[n]就是站n个单位空间的方法数,由于N比较大,所以很明显要用矩阵快速幂,写这篇题解同时提醒自己快速幂之前一定要判断指数是否非负。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0);
const int SIZE = + ;
const LL MOD = ; struct Matrix
{
int r, c;
LL mat[SIZE][SIZE];
Matrix() {}
Matrix(int _r, int _c)
{
r = _r;
c = _c;
memset(mat, , sizeof(mat));
}
}; Matrix operator*(Matrix a, Matrix b)
{
Matrix s(a.r, b.c);
for (int i = ; i < a.r; i++)
{
for (int k = ; k < a.c; k++)
{ //j, k交换顺序运行更快
for (int j = ; j < b.c; j++)
{
s.mat[i][j] = (s.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
}
}
}
return s;
} Matrix pow_mod(Matrix a, LL n)
{
Matrix ret(a.r, a.c);
for (int i = ; i < a.r; i++)
{
ret.mat[i][i] = ; //单位矩阵
}
Matrix tmp(a);
while (n)
{
if (n & )
ret = ret * tmp;
tmp = tmp * tmp;
n >>= ;
}
return ret;
} LL n, m; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n >> m;
if (n < m)
{
cout << << endl;
return ;
}
Matrix ori = Matrix(m, );
for (int i = ; i < m; i++)
{
ori.mat[i][] = unit;
}
Matrix A = Matrix(m, m);
A.mat[][] = unit;
A.mat[][m - ] = unit;
for (int i = ; i < m; i++)
{
A.mat[i][i - ] = unit;
}
Matrix ans = pow_mod(A, n - m + );
ori = ans * ori;
cout << ori.mat[][] << endl;
return ;
}

Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)的更多相关文章

  1. 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,所以要用到矩阵快速幂优化 ...

  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 60 (Rated for Div. 2)

    A. Best Subsegment 题意 找 连续区间的平均值  满足最大情况下的最长长度 思路:就是看有几个连续的最大值 #include<bits/stdc++.h> using n ...

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

  6. 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() ...

  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 60 (Rated for Div. 2) E. Decypher the String

    题目大意:这是一道交互题.给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3 ...

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

随机推荐

  1. Jmeter入门篇

    下载Jmeter:http://jmeter.apache.org/download_jmeter.cgi 安装后双击bin目录下的jmeter.bat文件启动jmeter 先看下怎么创建一个测试计划 ...

  2. Luogu P5290 [十二省联考2019]春节十二响

    这题是最近看到的今年省选题中最良心的一道了吧 看题+想题+写题都可以在0.5h内解决,送分含义明显啊 首先理解了题意后我们很快就能发现两个点如果要被分在一段那么必须在它们的祖先处合并 首先我们考虑下二 ...

  3. [AI开发]将深度学习技术应用到实际项目

    本文介绍如何将基于深度学习的目标检测算法应用到具体的项目开发中,体现深度学习技术在实际生产中的价值,算是AI算法的一个落地实现.本文算法部分可以参见前面几篇博客: [AI开发]Python+Tenso ...

  4. Intellij IDEA 阅读源码的 4 个绝技,我必须分享给你!

    前段时间分享了<阅读跟踪 Java 源码的几个小技巧>是基于 Eclipse 版本的,看大家的留言都是想要 IDEA 版本的源码阅读技巧. 所以,为了满足众多 IDEA 粉丝的要求,栈长我 ...

  5. python接口自动化(十一)--发送post【data】(详解)

    简介 前面登录博客园的是传 json 参数,由于其登录机制的改变没办法演示,然而在工作中有些登录不是传 json 的,如 jenkins 的登录,这里小编就以jenkins 登录为案例,传 data ...

  6. Bumblebee微服务网关的部署和扩展

    Bumblebee是.netcore下开源基于BeetleX.FastHttpApi扩展的HTTP微服务网关组件,它的主要作用是针对WebAPI集群服务作一个集中的转发和管理:作为应用网关它提供了应用 ...

  7. 禁用 Chrome 的黑色模式/Dark Mode

    macOS Mojave 中引入了系统层面的黑色模式,Chrome 73 在应用中支行了这一模式,即系统设置为黑色模式时,Chrome 会自动适应切换到 Dark Mode. Chrome 跟随系统设 ...

  8. 系列文章|OKR与敏捷(二):实现全栈敏捷

    OKR与敏捷开发的原理有着相似之处,但已经使用敏捷的团队再用OKR感觉会显得多余.这种误解的根源就在于对这两种模式不够了解,运用得当的情况下,OKR和敏捷可以形成强强联合的效果,他们可以创造出以价值为 ...

  9. 图像检索(4):IF-IDF,RootSift,VLAD

    TF-IDF RootSift VLAD TF-IDF TF-IDF是一种用于信息检索的常用加权技术,在文本检索中,用以评估词语对于一个文件数据库中的其中一份文件的重要程度.词语的重要性随着它在文件中 ...

  10. SpringEl表达式(转)

    什么是SpringEL? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构造函数当中 ...