CoderForces-Round60D(1117) Magic Gems
D. Magic Gems
3 seconds
256 megabytes
standard input
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.
The input contains a single line consisting of 22 integers NN and MM (1≤N≤10181≤N≤1018, 2≤M≤1002≤M≤100).
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).
4 2
5
3 2
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的更多相关文章
- 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 ...
- CF1117D Magic Gems
CF1117D Magic Gems 考虑 \(dp\) , \(f[i]\) 表示用 \(i\) 个单位空间的方案数,答案即为 \(f[n]\). 对于一个位置,我们可以放 \(Magic\) 的, ...
- [递推+矩阵快速幂]Codeforces 1117D - Magic Gems
传送门:Educational Codeforces Round 60 – D 题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...
- D. Magic Gems(矩阵快速幂 || 无敌杜教)
https://codeforces.com/contest/1117/problem/D 题解:有一些魔法宝石,魔法宝石可以分成m个普通宝石,每个宝石(包括魔法宝石)占用1个空间,让你求占用n个空间 ...
- 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 ...
- 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,所以要用到矩阵快速幂优化 ...
- eduCF#60 D. Magic Gems /// 矩阵快速幂
题目大意: 给定n m (1≤N≤1e18, 2≤M≤100) 一个魔法水晶可以分裂成连续的m个普通水晶 求用水晶放慢n个位置的方案modulo 1000000007 (1e9+7) input 4 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
随机推荐
- [LC]111题 二叉树的最小深度 (递归)
①题目 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15 ...
- Spring boot 加入shiro支持
在项目添加依赖 <!-- shiro spring. --> <dependency> <groupId>org.apache.shiro</groupId& ...
- 领扣(LeetCode)删除注释 个人题解
给一个 C++ 程序,删除程序中的注释.这个程序source是一个数组,其中source[i]表示第i行源码. 这表示每行源码由\n分隔. 在 C++ 中有两种注释风格,行内注释和块注释. 字符串// ...
- 领扣(LeetCode)移动零 个人题解
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...
- 领扣(LeetCode)合并两个有序数组 个人题解
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m 和 n. ...
- ubuntu 16.04上源码编译libjpeg-turbo和使用教程 | compile and use libjpeg-turbo on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/9f626e7a/,欢迎阅读! compile and use libjpeg-turbo on ubuntu 16.04 Seri ...
- 解决django或者其他线程中调用scrapy报ReactorNotRestartable的错误
官网中关于ReactorNotRestartable的错误描述(摘自:https://twistedmatrix.com/documents/16.1.0/api/twisted.internet.e ...
- React中使用create-react-app创建项目,运行npm run eject建立灰度报错
我在运行npm run eject建立测试环境和正式环境时候报错 这里的问题是是脚手架添加.gitgnore文件,但是却没有本地仓库,按照以下顺序就可以正常使用 git add . git commi ...
- nginx-(三)基本模块1
nginx常用模块介绍 ngx_http_access_module模块的配置(基于IP的访问控制) allow address | CIDR | unix: | all; deny address ...
- 内核升级在线安装报错:Could not retrieve mirrorlist http://mirrors.elrepo.org/mirrors-elrepo-kernel.el7 error was14: curl#6 - "Could not resolve host: mirrors.elrepo.org; 未知的错误"
修改网卡配置 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 BOOTPROTO="none" ...