CF755G PolandBall and Many Other Balls 题解

从神 Karry 的题单过来的,然后自己瞎 yy 了一个方法,看题解区里没有,便来写一个题解

一个常数和复杂度都很大的题解


(dp_{i,j}) 为 在 (i) 个球中选 (j) 组的方案数,则显然有转移 (dp_{i,j}=dp_{i-1,j}+dp_{i-1,j-1}+dp_{i-2,j-1})

然后考虑对其优化:
(f_i)(dp_i) 的生成函数,则 (f_i) 只与 (f_{i-1})(f_{i-2}) 有关,且关系为 (f_i=f_{i-2}cdot x + f_{i-1}cdot (x+1)) .
然后考虑对上式进行矩阵加速递推,复杂度 (O(k log k log n)),再加一个巨大的常数,但此题 (k) 的范围很小,可以通过。

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define fir first
#define sec second
#define mod 998244353
#define ll long long
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
typedef pair<int,int> pii;
int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x,int y)
{
	int ans=1;
	while(y)
	{
		if(y&1) ans=mul(ans,x);
		x=mul(x,x);
		y>>=1;
	}
	return ans;
}
int getInv(int x)
{
	return qpow(x,mod-2);
}
// 略去多项式的板子,以下 vector 即是存储多项式的容器
using namespace Poly;
int n,k;
struct Mat
{
	vector<int> a[3][3];
	void clear()
	{
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				a[i][j].resize(1);
				a[i][j][0]=0;
			}
		}
	}
};
Mat mul(Mat x,Mat y)
{
	Mat ans; ans.clear();
	for(int i=1;i<=2;i++)
	{
		for(int j=1;j<=2;j++)
		{
			for(int k=1;k<=2;k++)
			{
				ans.a[i][j]=ans.a[i][j]+x.a[i][k]*y.a[k][j];
			}
		}
	}
	for(int i=1;i<=2;i++)
	{
		for(int j=1;j<=2;j++)
		{
			if((int)ans.a[i][j].size()>k+1) ans.a[i][j].resize(k+1);
		}
	}
	return ans;
}
Mat qpow(Mat x,int y)
{
	Mat ans; ans.clear();
	ans.a[1][1]=one,ans.a[2][2]=one;
	while(y)
	{
		if(y&1)
		{
			ans=mul(ans,x);
		}
		x=mul(x,x);
		y>>=1;
	}
	return ans;
}
signed main()
{
	Init_Inv();
	cin>>n>>k;
	Mat a; a.clear();
	a.a[1][2].resize(2); a.a[1][2][1]=1;
	a.a[2][1]=one;
	a.a[2][2].resize(2); a.a[2][2][1]=a.a[2][2][0]=1;
	a=qpow(a,n-1);
	Mat R; R.clear();
	R.a[1][1]=one;
	R.a[1][2].resize(2); R.a[1][2][0]=1,R.a[1][2][1]=1;
	R=mul(R,a);
	vector<int> ans=R.a[1][2];
	ans.resize(k+1);
	for(int i=1;i<=k;i++) printf("%d ",ans[i]); cout<<"
";
	return 0;
}


原文地址:https://www.cnblogs.com/wasa855/p/12376851.html