結果

問題 No.2499 Sum of Products of Sums
ユーザー kotatsugamekotatsugame
提出日時 2023-10-06 23:11:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,628 bytes
コンパイル時間 2,358 ms
コンパイル使用メモリ 114,004 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-06 23:11:36
合計ジャッジ時間 4,131 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 68 ms
4,380 KB
testcase_14 AC 62 ms
4,380 KB
testcase_15 AC 58 ms
4,380 KB
testcase_16 AC 73 ms
4,380 KB
testcase_17 AC 115 ms
4,376 KB
testcase_18 AC 116 ms
4,376 KB
testcase_19 AC 115 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cassert>
#include<atcoder/modint>
#include<atcoder/convolution>
using namespace std;
#include<vector>
template<typename T>
struct combination{
	vector<T>fac,ifac;
	combination(size_t N=0):fac(1,1),ifac(1,1)
	{
		make_table(N);
	}
	void make_table(size_t N)
	{
		if(fac.size()>N)return;
		size_t now=fac.size();
		N=max(N,now*2);
		fac.resize(N+1);
		ifac.resize(N+1);
		for(size_t i=now;i<=N;i++)fac[i]=fac[i-1]*i;
		ifac[N]=1/fac[N];
		for(size_t i=N;i-->now;)ifac[i]=ifac[i+1]*(i+1);
	}
	T factorial(size_t n)
	{
		make_table(n);
		return fac[n];
	}
	T invfac(size_t n)
	{
		make_table(n);
		return ifac[n];
	}
	T P(size_t n,size_t k)
	{
		if(n<k)return 0;
		make_table(n);
		return fac[n]*ifac[n-k];
	}
	T C(size_t n,size_t k)
	{
		if(n<k)return 0;
		make_table(n);
		return fac[n]*ifac[n-k]*ifac[k];
	}
	T H(size_t n,size_t k)
	{
		if(n==0)return k==0?1:0;
		return C(n-1+k,k);
	}
};
using mint=atcoder::modint998244353;
combination<mint>C;
mint comb(int A,int B)
{
	assert(B>=0);
	if(A<B)return mint(0);
	mint x=1,y=1;
	for(int i=1;i<=B;i++)
	{
		x*=A+1-i;
		y*=mint::raw(i);
	}
	return x/y;
}
int H,W;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>H>>W;
	vector<mint>dp(1,1);
	for(int i=0;i<H;i++)
	{
		int A,B;cin>>A>>B;
		vector<mint>now(W+1);
		mint CB=comb(B+W,W);
		mint CA=comb(A-1+W,W);
		for(int j=0;j<=W;j++)
		{
			//CB=C(B+W,W+j)
			now[j]=CB-CA;
			mint iv=C.factorial(W+j)*C.invfac(W+j+1);
			CB=CB*(B-j)*iv;
			CA=CA*(A-1-j)*iv;
			now[j]*=C.invfac(j);
		}
		dp=atcoder::convolution(dp,now);
		dp.resize(W+1);
	}
	cout<<(dp[W]*C.factorial(W)).val()<<endl;
}
0