D. Magic Gems

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Reziba has many magic gems. Each magic gem can be split into MM normal gems. The amount of space each magic (and normal) gem takes is 11 unit. A normal gem cannot be split.

Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is NN units. If a magic gem is chosen and split, it takes MM units of space (since it is split into MM gems); if a magic gem is not split, it takes 11 unit.

How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is NN units? Print the answer modulo 10000000071000000007 (109+7109+7). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ.

Input

The input contains a single line consisting of 22 integers NN and MM (1≤N≤10181≤N≤1018, 2≤M≤1002≤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 NN units. Print the answer modulo 10000000071000000007 (109+7109+7).

Examples
input

Copy
4 2
output

Copy
5
input

Copy
3 2
output

Copy
3

In the first example each magic gem can split into 22 normal gems, and we know that the total amount of gems are 44.

Let 11 denote a magic gem, and 00 denote a normal gem.

The total configurations you can have is:

  • 11111111 (None of the gems split);
  • 00110011 (First magic gem splits into 22 normal gems);
  • 10011001 (Second magic gem splits into 22 normal gems);
  • 11001100 (Third magic gem splits into 22 normal gems);
  • 00000000 (First and second magic gems split into total 44 normal gems).

Hence, answer is 55.

题解:

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

也可以用杜教BM,求线性递推式;

参考代码:(矩阵快速幂)

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Mod 1000000007
const double PI = acos(-1.0);
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const int N = + ;
struct Matrix {
ll n , m;
ll grid[N][N];
Matrix () { n = m = ; memset(grid , , sizeof(grid)); }
}; Matrix mul(Matrix a,Matrix b)
{
Matrix c;
c.n = a.n;c.m = b.m;
for(ll i=;i<=c.n;++i)
for(ll j=;j<=c.m;++j)
{
ll cnt = ;
for(ll k=;k<=a.m;++k)
{
c.grid[i][j] = (c.grid[i][j] + a.grid[i][k] * b.grid[k][j]);
cnt++;
if(cnt % == ) c.grid[i][j] %= Mod;
}
c.grid[i][j] %= Mod;
}
return c;
}
Matrix QuickMul(Matrix a ,ll k)
{
if(k == ) return a;
Matrix mid = QuickMul(a ,(k >> ));
if(k & ) return mul(mul(mid , mid),a);
else return mul(mid , mid);
}
ll n , m;
int main()
{
cin >> n >> m;
if(n < m) {return puts("") , ;}
if(n == m) return puts("") , ;
Matrix basic; basic.n = m; basic.m = ;
for(ll i=;i<=m;++i) basic.grid[i][] = (i == m) ? : ;//{1,1,1...1,m}T
Matrix base; base.n = base.m = m; for(ll i = ; i <= m - ; i++) base.grid[i][i + ] = ;
base.grid[m][] = base.grid[m][m] = ; Matrix ans = mul(QuickMul(base , n - m) , basic);
cout << ans.grid[m][] << endl;
return ;
}

杜教BM

 #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
ll powmod(ll a,ll b) {ll res=;a%=mod; assert(b>=); for(;b;b>>=) { if(b&)res=res*a%mod; a=a*a%mod; } return res; }
ll _,n,m,dp[];
namespace linear_seq {
const int N=;
ll res[N],base[N],_c[N],_md[N];
vector<ll> Md;
void mul(ll *a,ll *b,int k)
{
rep(i,,k+k) _c[i]=;
rep(i,,k) if (a[i]) rep(j,,k) _c[i+j]= (_c[i+j]+a[i]*b[j])%mod;
for (int i=k+k-;i>=k;i--) if (_c[i])
rep(j,,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
rep(i,,k) a[i]=_c[i];
}
int solve(ll n,VI a,VI b)
{
ll ans=,pnt=;
int k=SZ(a);
assert(SZ(a)==SZ(b));
rep(i,,k) _md[k--i]=-a[i];_md[k]=;
Md.clear();
rep(i,,k) if (_md[i]!=) Md.push_back(i);
rep(i,,k) res[i]=base[i]=;
res[]=;
while ((1ll<<pnt)<=n) pnt++;
for (int p=pnt;p>=;p--)
{
mul(res,res,k);
if ((n>>p)&)
{
for (int i=k-;i>=;i--) res[i+]=res[i];res[]=;
rep(j,,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
}
}
rep(i,,k) ans=(ans+res[i]*b[i])%mod;
if (ans<) ans+=mod;
return ans;
}
VI BM(VI s) {
VI C(,),B(,);
int L=,m=,b=;
rep(n,,SZ(s)) {
ll d=;
rep(i,,L+) d=(d+(ll)C[i]*s[n-i])%mod;
if (d==) ++m;
else if (*L<=n) {
VI T=C;
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
rep(i,,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
L=n+-L; B=T; b=d; m=;
} else {
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
rep(i,,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
++m;
}
}
return C;
}
int gao(VI a,ll n) {
VI c=BM(a);
c.erase(c.begin());
rep(i,,SZ(c)) c[i]=(mod-c[i])%mod;
return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
}
};
int main()
{
scanf("%lld%lld",&n,&m);
vector<int> v;
for(int i=;i<m;++i) v.push_back();
for(ll i=;i<=m;++i) dp[i]=i+,v.push_back(dp[i]);
for(int i=m+;i<=;++i) dp[i]=dp[i-]+dp[i-m],v.push_back(dp[i]); printf("%lld\n",linear_seq::gao(v,n-)%mod);
return ;
}

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

    CF1117D Magic Gems 考虑 \(dp\) , \(f[i]\) 表示用 \(i\) 个单位空间的方案数,答案即为 \(f[n]\). 对于一个位置,我们可以放 \(Magic\) 的, ...

  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. Educational Codeforces Round 60 (Rated for Div. 2) 题解

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

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

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

随机推荐

  1. Java ------ 工厂模式、单例模式

    工厂模式 简单工厂模式: 1.创建Car接口 public interface Car { public void drive(); } 2.创建两个实体类,分别实现Car接口 public clas ...

  2. 本地存储localstorage

    小小插件,封装了一个存取删 <script type="text/javascript"> /* *getItem(name) * *setItem(name,valu ...

  3. 6. SOFAJRaft源码分析— 透过RheaKV看线性一致性读

    开篇 其实这篇文章我本来想在讲完选举的时候就开始讲线性一致性读的,但是感觉直接讲没头没尾的看起来比比较困难,所以就有了RheaKV的系列,这是RheaKV,终于可以讲一下SOFAJRaft的线性一致性 ...

  4. java本地缓存

    1.为什么要使用缓存 由于服务器.数据库.网络等资源有限,无法支撑越来越多的请求与计算量,所以将一部分数据放在缓存中,以此减小薄弱环节的计算量和请求流程. 网站中缓存的应用场景:        1:可 ...

  5. 正则表达式 解决python2升python3的语法问题

      2019.9.12 更新   今天偶然看到 python 官网中,还介绍了一个专门的工具,用于 python2 升级 python3,以后有机会使用下看看 https://docs.python. ...

  6. vue 实现单选/多选效果

    转:https://blog.csdn.net/Number7421/article/details/81002729 不过我以前都写过这三种方法了,很pang额,怕之后忘记了,偷个懒拿别人的,以免以 ...

  7. nyoj 19-擅长排列的小明(STL-next_permutation())

    19-擅长排列的小明 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:10 submit:16 题目描述: 小明十分聪明,而且十分擅长排列计算.比如给 ...

  8. nyoj 477-A+B Problem III (fabs() <= 0.00001)

    477-A+B Problem III 内存限制:64MB 时间限制:1000ms 特判: No 通过数:18 提交数:34 难度:1 题目描述: 求A+B是否与C相等. 输入描述: T组测试数据. ...

  9. 力扣(LeetCode)字符串中的第一个唯一字符 个人题解

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...

  10. 利用Python学习线性代数 -- 1.1 线性方程组

    利用Python学习线性代数 -- 1.1 线性方程组 本节实现的主要功能函数,在源码文件linear_system中,后续章节将作为基本功能调用. 线性方程 线性方程组由一个或多个线性方程组成,如 ...