cf Educational Codeforces Round 60 D. Magic Gems

原题:
D. Magic Gems

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Reziba has many magic gems. Each magic gem can be split into M normal gems. The amount of space each magic (and normal) gem takes is 1 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 N units. If a magic gem is chosen and split, it takes M units of space (since it is split into M gems); if a magic gem is not split, it takes 1 unit.

How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is N units? Print the answer modulo 1000000007 (109+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 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 (10^9+7).

Examples
input
4 2
output
5
input
3 2
output
3
Note
In the first example each magic gem can split into 2 normal gems, and we know that the total amount of gems are 4.

Let 1 denote a magic gem, and 0 denote a normal gem.

The total configurations you can have is:

1111 (None of the gems split);
0011 (First magic gem splits into 2 normal gems);
1001 (Second magic gem splits into 2 normal gems);
1100 (Third magic gem splits into 2 normal gems);
0000 (First and second magic gems split into total 4 normal gems).
Hence, answer is 5.

中文:
给你一堆个魔法宝石一字排开,每个魔法宝石可以变成m个普通宝石,现在问你构造长度为n的宝石摆放有多少种方式?可以看第一个样例和解释。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll mod = 1000000007;
const int maxn = 100;

typedef vector<ll> vec;
typedef vector<vec> mat;



mat mul(mat &A,mat &B)
{
	mat C(A.size(),vec(B[0].size()));
	for(int i=0;i<A.size();i++)
	{
		for(int k=0;k<B.size();k++)
		{
			for(int j=0;j<B[0].size();j++)
			{
				C[i][j]=(C[i][j]+A[i][k]*B[k][j]);
				if(C[i][j]>=mod)
					C[i][j]%=mod;
			}
		}
	}
	return C;
}


mat pow_mat(mat &A,ll n)
{
	mat B(A.size(),vec(A.size()));
	for(int i=0;i<A.size();i++)
	{
		B[i][i]=1;
	}
	while(n>0)
	{
		if(n&1)B = mul(B,A);
		A = mul(A,A);
		n>>=1;
	}
	return B;
}



int main()
{

	ios::sync_with_stdio(false);
	ll n,m;
	while(cin>>n>>m)
	{

		mat M(m,vec(m));
		M[0][0]=M[0][m-1]=1;

        for(int i=1;i<m;i++)
			M[i][i-1]=1;

		if(n<m)
		{
			cout<<1<<endl;
			continue;
		}
        M = pow_mat(M,n-m+1);

		ll ans=0;
		for(int i=0;i<m;i++)
        {
            ans+=M[0][i];
            if(ans>=mod)
                ans-=mod;
        }

		cout<<ans<<endl;

	}
	return 0;
}


解答:

如果对组合数学或者递推比较熟悉,很快就能列出这道题的组合公式或者递推公式。如果考虑使用组合公式计算,可以看做先在桌子上摆放n个魔法宝石,取某些连续的m个魔法宝石替换成普通宝石,这样就是一种组合方式了。
公式也很简单:

∑ k = 0 ( n − k ( m − 1 ) k ) \sum\limits_{k=0}\binom{n-k(m-1)}{k} k=0(knk(m1))

但是看数据,可以发现n的取值范围达到了10^18,根本不可能使用任何遍历的手段实现。如果考虑上面的组合数公式是否能够变形或者优化成一个能在O(1)方法实现,那此题就成功的套住你了,毕竟解决10 ^18数据问题的方法很少会去找处理过繁琐的方法(我就被套住了-_-|||)

如果考虑用递推的方式解决,那么状态转移方程也是非常简单:
d p [ n ] = d p [ n − m ] + d p [ n − 1 ] dp[n]=dp[n-m]+dp[n-1] dp[n]=dp[nm]+dp[n1]
表示长度为n的排列宝石,的组合方式可以来自于在n-1宝石排列中添加一个宝石,以及在n-m个宝石中,添加一个魔法宝石。

处理至此,仍然会发现数据n的数量级根本无法保存状态,咋办?

如果看到题解恍然大悟立刻知道怎么做的话,那你肯定是做过这样一道题,给你一个斐波那契数列,让你快速找出该斐波那契数列的第n项,这个n可能非常大,在不用通项公式的情况,有这样一种办法,将递推关系写成矩阵的形式,然后利用矩阵相乘的递推关系得到变换矩阵A的x次幂乘以初项即可快速得到最终结果。如此一来可以利用快速幂方法处理高次方,就能得到答案。

本题的矩阵递推方法如下:

根据上面的递推方程,可以写成下面的形式

[ d p n d p n − 1 . . . d p n − m + 1 ] = [ 1 0 0 0 . . . 1 1 0 0 0 . . . 0 . . . 0 0 0 . . . 1 0 ] [ d p n − 1 d p n − 2 . . . d p n − m ] \begin{bmatrix} dp_n\\ dp_{n-1}\\ ...\\ dp_{n-m+1} \end{bmatrix} = \begin{bmatrix} 1&amp;0&amp;0&amp;0&amp;...&amp;1\\ 1&amp;0&amp;0&amp;0&amp;...&amp;0\\ &amp;&amp;...\\ 0&amp;0&amp;0&amp;...&amp;1&amp;0 \end{bmatrix} \begin{bmatrix} dp_{n-1}\\ dp_{n-2}\\ ...\\ dp_{n-m} \end{bmatrix} dpndpn1...dpnm+1=11000000...000.........1100dpn1dpn2...dpnm
那么,如果要计算 d p n dp_n dpn
[ d p n d p n − 1 . . . d p n − m + 1 ] = [ 1 0 0 0 . . . 1 1 0 0 0 . . . 0 . . . 0 0 0 . . . 1 0 ] n − m + 1 [ d p m − 1 d p m − 2 . . . d p 0 ] \begin{bmatrix} dp_n\\ dp_{n-1}\\ ...\\ dp_{n-m+1} \end{bmatrix} = \begin{bmatrix} 1&amp;0&amp;0&amp;0&amp;...&amp;1\\ 1&amp;0&amp;0&amp;0&amp;...&amp;0\\ &amp;&amp;...\\ 0&amp;0&amp;0&amp;...&amp;1&amp;0 \end{bmatrix}^{n-m+1} \begin{bmatrix} dp_{m-1}\\ dp_{m-2}\\ ...\\ dp_{0} \end{bmatrix} dpndpn1...dpnm+1=11000000...000.........1100nm+1dpm1dpm2...dp0

时间复杂度是O(m^3logn),也不小-_-|||

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值