結果

問題 No.2514 Twelvefold Way Returns
ユーザー shobonvipshobonvip
提出日時 2023-10-20 23:11:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,534 ms / 3,000 ms
コード長 6,265 bytes
コンパイル時間 5,513 ms
コンパイル使用メモリ 282,892 KB
実行使用メモリ 14,376 KB
最終ジャッジ日時 2024-09-20 22:28:11
合計ジャッジ時間 41,159 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
14,248 KB
testcase_01 AC 128 ms
14,124 KB
testcase_02 AC 326 ms
14,248 KB
testcase_03 AC 155 ms
14,124 KB
testcase_04 AC 1,264 ms
14,248 KB
testcase_05 AC 1,108 ms
14,120 KB
testcase_06 AC 939 ms
14,120 KB
testcase_07 AC 93 ms
14,248 KB
testcase_08 AC 702 ms
14,116 KB
testcase_09 AC 690 ms
14,252 KB
testcase_10 AC 1,392 ms
14,248 KB
testcase_11 AC 148 ms
14,244 KB
testcase_12 AC 377 ms
14,248 KB
testcase_13 AC 103 ms
14,376 KB
testcase_14 AC 319 ms
14,252 KB
testcase_15 AC 597 ms
14,248 KB
testcase_16 AC 1,345 ms
14,248 KB
testcase_17 AC 899 ms
14,244 KB
testcase_18 AC 1,443 ms
14,248 KB
testcase_19 AC 1,496 ms
14,252 KB
testcase_20 AC 1,477 ms
14,248 KB
testcase_21 AC 1,504 ms
14,248 KB
testcase_22 AC 1,494 ms
14,248 KB
testcase_23 AC 622 ms
14,244 KB
testcase_24 AC 1,313 ms
14,248 KB
testcase_25 AC 690 ms
14,244 KB
testcase_26 AC 91 ms
14,120 KB
testcase_27 AC 1,410 ms
14,244 KB
testcase_28 AC 1,384 ms
14,244 KB
testcase_29 AC 1,088 ms
14,120 KB
testcase_30 AC 1,316 ms
14,124 KB
testcase_31 AC 1,422 ms
14,248 KB
testcase_32 AC 689 ms
14,252 KB
testcase_33 AC 93 ms
14,244 KB
testcase_34 AC 320 ms
14,248 KB
testcase_35 AC 1,370 ms
14,244 KB
testcase_36 AC 92 ms
14,120 KB
testcase_37 AC 91 ms
14,252 KB
testcase_38 AC 1,508 ms
14,116 KB
testcase_39 AC 1,534 ms
14,248 KB
testcase_40 AC 1,425 ms
14,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

int calc(int a, int b, int c){
	return a*a*a + b*b*b + c*c*c;
}

//defmodfact
const int COMinitMAX = 998244;
mint fact[COMinitMAX+1], factinv[COMinitMAX+1];

void modfact(){
	fact[0] = 1;
	for (int i=1; i<=COMinitMAX; i++){
		fact[i] = fact[i-1] * i;
	}
	factinv[COMinitMAX] = fact[COMinitMAX].inv();
	for (int i=COMinitMAX-1; i>=0; i--){
		factinv[i] = factinv[i+1] * (i+1);
	}
}

mint cmb(int a, int b){
	if (a<b || b<0) return mint(0);
	return fact[a]*factinv[b]*factinv[a-b];
}
//--------

vector<mint> poly_inv(vector<mint> &a, int M = -314159265){
	if (M == -314159265) M = (int)a.size();
	else if (M <= 0) return {};
	int n = a.size();
	mint r = a[0].pow((ll)(mint::mod())-2);
	int m = 1;
	vector<mint> res = {r};
	while (m < M){
		vector<mint> f = a;
		f.resize(2 * m);
		vector<mint> g = res;
		g.resize(2 * m);
		internal::butterfly(f);
		internal::butterfly(g);
		for (int i=0; i<2*m; i++){
			f[i] = f[i] * g[i];
		}
		internal::butterfly_inv(f);
		for (int i=0; i<m; i++){
			f[i] = f[i + m];
		}
		for (int i=0; i<m; i++){
			f[i + m] = 0;
		}
		internal::butterfly(f);
		for (int i=0; i<2*m; i++){
			f[i] = f[i] * g[i];
		}
		internal::butterfly_inv(f);
		mint iz = mint(2*m).inv();
		iz = -iz * iz;
		for (int i=0; i<m; i++){
			f[i] = f[i] * iz;
		}
		res.insert(res.end(), f.begin(), f.begin()+m);
		m <<= 1;
	}
	res.resize(M);
	return res;
}

vector<mint> poly_log(vector<mint> &a, int M = -314159265){
	if (M == -314159265) M = (int)a.size();
	else if (M <= 0) return {};
	int n = a.size();
	if (n == 1) return vector<mint>(M, 0);
	vector<mint> b(n-1);
	for (int i=0; i<n-1; i++){
		b[i] = a[i+1] * (i+1);
	}
	vector<mint> t = convolution<mint>(b, poly_inv(a, M));
	vector<mint> ret(M);
	for (int i=0; i<M-1; i++){
		ret[i+1] = t[i] * factinv[i+1] * fact[i];
	}
	return ret;
}

vector<mint> poly_exp(vector<mint> &a, int M = -314159265){
	if (M == -314159265) M = (int)a.size();
	else if (M <= 0) return {};
	int n = a.size();
	int m = 1;
	vector<mint> res = {1};
	while (m < M){
		vector<mint> f(2*m);
		for (int i=0; i<min(n, 2*m); i++) f[i] = a[i];
		vector<mint> v = poly_log(res, 2*m);
		vector<mint> w(2*m);
		for (int i=0; i<2*m; i++) w[i] = f[i] - v[i];
		w[0] += 1;
		vector<mint> g = convolution<mint>(res, w);
		res.insert(res.end(), g.begin()+m, g.begin()+2*m);
		m <<= 1;
	}
	res.resize(M);
	return res;
}

vector<mint> poly_pow_nonzero(vector<mint> &a, ll m, ll l){
	int n = a.size();
	mint bais = a[0].pow(m);
	mint invs;
	if (a[0].val() == 0) invs = 0;
	else invs = a[0].inv();
	vector<mint> r(n);
	for (int i=0; i<n; i++) r[i] = a[i] * invs;
	r = poly_log(r, l);
	for (int i=0; i<l; i++) r[i] = r[i] * m;
	r = poly_exp(r, l);
	for (int i=0; i<l; i++) r[i] = r[i] * bais;
	return r;
}

vector<mint> poly_pow(vector<mint> &a, ll m, ll l){
	int n = a.size();
	int ind = 0;
	for (int i=0; i<n; i++){
		if (a[i].val() != 0){
			ind = i;
			break;
		}
	}
	ll g = min(m, (ll)3141592653);
	if (g*ind >= l){
		return vector<mint>(l, 0);
	}
	vector<mint> ret(g*ind);
	vector<mint> b(n-ind);
	for (int i=0; i<n-ind; i++) b[i] = a[i+ind];
	vector<mint> tmp;
	if (l-g*ind > 0) tmp = poly_pow_nonzero(b, m, l-g*ind);
	else tmp = {};
	ret.insert(ret.end(), tmp.begin(), tmp.end());
	return ret;
}

vector<mint> BerlekampMassey(const vector<mint> &s) {
	const int N = (int)s.size();
	vector<mint> b, c;
	b.reserve(N + 1);
	c.reserve(N + 1);
	b.push_back(mint(1));
	c.push_back(mint(1));
	mint y = mint(1);
	for (int ed = 1; ed <= N; ed++) {
		int l = int(c.size()), m = int(b.size());
		mint x = 0;
		for (int i = 0; i < l; i++) x += c[i] * s[ed - l + i];
		b.emplace_back(mint(0));
		m++;
		if (x == mint(0)) continue;
		mint freq = x / y;
		if (l < m) {
			auto tmp = c;
			c.insert(begin(c), m - l, mint(0));
			for (int i = 0; i < m; i++) c[m - 1 - i] -= freq * b[m - 1 - i];
			b = tmp;
			y = x;
		} else {
			for (int i = 0; i < m; i++) c[l - 1 - i] -= freq * b[m - 1 - i];
		}
	}
	reverse(begin(c), end(c));
	return c;
}


mint bostan_mori(ll n, vector<mint> p, vector<mint> q){
	assert(p.size() < q.size());
	while (n > 0){
		vector<mint> qi((int)q.size());
		for (int i=0; i<(int)q.size(); i++){
			if (i%2==0) qi[i] = q[i];
			else qi[i] = -q[i];
		}
		vector<mint> qq = convolution<mint>(q, qi);
		q.resize(((int)qq.size()+1)/2);
		for (int i=0; i<((int)qq.size()+1)/2; i++){
			q[i] = qq[2*i];
		}
		vector<mint> pp = convolution<mint>(p, qi);
		if (n%2==0){
			p.resize(((int)pp.size()+1)/2);
			for (int i=0; i<((int)pp.size()+1)/2; i++){
				p[i] = pp[2*i];
			}
		}else{
			p.resize((int)pp.size()/2);
			for (int i=0; i<(int)pp.size()/2; i++){
				p[i] = pp[2*i+1];
			}
		}
		n/=2;
	}
	return p[0]*q[0].inv();
}


int main(){
	modfact();
	int n, m; cin >> n >> m;

	int mx = 40000;
	vector<mint> f(mx+1);
	rep(i,0,mx+1){
		f[i] = factinv[3*i+1];
	}
	
	vector<mint> g = poly_pow(f, m, mx+1);
	rep(i,0,mx+1){
		g[i] *= fact[3*i+m];
	}
	
	vector<mint> bm = BerlekampMassey(g);
	//rep(i,0,(int)bm.size()){
	//	cout << i << ' ' << bm[i].val() << '\n';
	//}
	
	g.resize((int)bm.size() - 1);
	vector<mint> h = convolution<mint>(g, bm);
	
	h.resize((int)bm.size() - 1);
	if ((n-m+3*m)%3 == 0 && n-m >= 0){
		cout << bostan_mori((n-m)/3, h, bm).val() << '\n';
	}else{
		cout << 0 << '\n';
	}
}

0