跳到正文

题解

题解:AT_fps_24_a お菓子

bcdmwSjy约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年11月6日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟
获赞
10

版本

  1. v2当前
  2. v1

算法 1:

列出每一天的生成函数,为 x+x3+x4+x6x+x^3+x^4+x^6。

共有 DD 天,答案即为 [xN](x+x3+x4+x6)D[x^N](x+x^3+x^4+x^6)^D,直接多项式快速幂即可,时间复杂度为 O(Nlog⁡N)O(N\log N),常数有点大。

代码不给了。

算法 2:

仍然使用上面的生成函数,因式分解得到 x+x3+x4+x6=x(1+x2)(1+x3)x+x^3+x^4+x^6=x(1+x^2)(1+x^3),就相当于计算 (1+x2)D(1+x3)D(1+x^2)^D(1+x^3)^D 的 xN−Dx^{N-D} 项系数,二项式定理展开得

(1+x2)D(1+x3)D=(∑i=0(Di)x2i)(∑j=0(Dj)x3j)(1+x^2)^D(1+x^3)^D=\left(\sum\limits_{i=0}\binom Dix^{2i}\right)\left(\sum\limits_{j=0}\binom Djx^{3j}\right)

枚举 ii,则 3j3j 等于 N−D−2iN-D-2i,计算即可。

时间复杂度为 O(D)O(D)。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int mod=998244353;

int n,d;
ll inv[200001],fac[200001],ifac[200001];

inline ll C(int n,int m){
	if (n<m or n<0 or m<0) return 0;
	return fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>d>>n;
	if (n<d){
		cout<<"0\n";
		return 0;
	}
	inv[1]=fac[0]=fac[1]=ifac[0]=ifac[1]=1;
	for (int i=2;i<=d;i++){
		inv[i]=(mod-mod/i)*inv[mod%i]%mod;
		fac[i]=fac[i-1]*i%mod;
		ifac[i]=ifac[i-1]*inv[i]%mod;
	}
	int ans=0;
	for (int i=0;i<=d;i++){
		int t=n-d-i-i;
		if (t>=0 and t%3==0){
			ans=(ans+C(d,i)*C(d,t/3))%mod;
		}
	}
	cout<<ans;
	return 0;
}

5ms 跑完,比多项式快 40 多倍。

原文