結果

問題 No.137 貯金箱の焦り
ユーザー HIR180HIR180
提出日時 2020-04-19 12:20:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,206 ms / 5,000 ms
コード長 4,207 bytes
コンパイル時間 3,288 ms
コンパイル使用メモリ 213,080 KB
実行使用メモリ 18,092 KB
最終ジャッジ日時 2024-04-15 08:32:05
合計ジャッジ時間 36,300 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,199 ms
17,776 KB
testcase_01 AC 1,206 ms
17,776 KB
testcase_02 AC 1,194 ms
17,776 KB
testcase_03 AC 1,192 ms
17,904 KB
testcase_04 AC 1,192 ms
18,032 KB
testcase_05 AC 1,197 ms
17,776 KB
testcase_06 AC 1,197 ms
18,028 KB
testcase_07 AC 1,186 ms
17,908 KB
testcase_08 AC 1,194 ms
18,032 KB
testcase_09 AC 1,194 ms
17,780 KB
testcase_10 AC 1,195 ms
18,032 KB
testcase_11 AC 1,191 ms
18,028 KB
testcase_12 AC 1,197 ms
17,776 KB
testcase_13 AC 1,194 ms
17,780 KB
testcase_14 AC 1,201 ms
17,904 KB
testcase_15 AC 1,197 ms
18,092 KB
testcase_16 AC 1,195 ms
17,904 KB
testcase_17 AC 1,199 ms
17,776 KB
testcase_18 AC 1,200 ms
17,724 KB
testcase_19 AC 1,200 ms
17,908 KB
testcase_20 AC 1,195 ms
18,028 KB
testcase_21 AC 1,202 ms
17,904 KB
testcase_22 AC 1,191 ms
17,900 KB
testcase_23 AC 1,194 ms
17,780 KB
testcase_24 AC 1,192 ms
17,904 KB
testcase_25 AC 1,196 ms
18,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i, n) for(int i=1;i<=n;i++)
#define all(x) x.begin(), x.end()
#define pb push_back
typedef pair<int,int> P;
#define fi first
#define sc second
#define mp make_pair
typedef long long ll;
const ll mod = 1234567891;

template<class T, const T md>
struct ntt{
	inline void add(T &a, T b) { a += b; if(a >= md) a -= md; }
	inline void sub(T &a, T b) { a -= b; if(a < 0) a += md; }
	inline T add2(T a, T b) { a += b; if(a >= md) a -= md; return a; }
	inline T sub2(T a, T b) { return add2(a, md-b); }
	inline T mul(T a, T b) { return (T)((ll)a*b%md); }
	inline T power(T a, long long b) {
		T res = 1;
		while (b > 0) {
			if (b & 1) res = mul(res, a);
	    	a = mul(a, a);
			b >>= 1;
		}
		return res;
	}
	inline T inv(T a) {
		a %= md;
		if (a < 0) a += md;
		T b = md, u = 0, v = 1;
		while (a) {
			T t = b / a;
			b -= t * a; swap(a, b);
			u -= t * v; swap(u, v);
		}
		assert(b == 1);
		if (u < 0) u += md;
		return u;
	}
	
 	T max_base, root;
	vector<T> dw, idw;
	ntt() {
		T tmp = md - 1;
		max_base = 0;
		while (tmp % 2 == 0) {
			tmp /= 2;
			max_base++;
		}
		root = 2;
		while (power(root, (md-1)>>1) == 1) root++;
		dw.resize(max_base); idw.resize(max_base);
		rep(i, max_base){
			sub(dw[i], power(root, (md-1) >> (i+2)));
			idw[i] = inv(dw[i]);
		}
	}
	void fft(vector<T> &a, bool inv) {
		const int n = a.size();
		assert((n & (n - 1)) == 0);
		assert(__builtin_ctz(n) <= max_base);
		if(!inv){
			for(int m=n;m>>=1;){
				T w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						T x = a[i], y = mul(a[j], w);
						a[j] = (x>=y?x-y:x+md-y);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, dw[__builtin_ctz(++k)]);
				}
			}
		}
		else{
			for(int m=1;m<n;m*=2){
				T w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						T x = a[i], y = a[j];
						a[j] = (x>=y?x-y:x+md-y);
						a[j] = mul(a[j], w);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, idw[__builtin_ctz(++k)]);
				}
			}
		}
	}
	vector<T> multiply(vector<T> a, vector<T> b, int eq = 0) {
		int need = a.size() + b.size() - 1;
		int nbase = 0;
		while ((1 << nbase) < need) nbase++;
		int sz = 1 << nbase;
		a.resize(sz);
		b.resize(sz);
		fft(a, 0);
		if (eq) b = a; else fft(b, 0);
		int inv_sz = inv(sz);
		for (int i = 0; i < sz; i++) {
			a[i] = mul(mul(a[i], b[i]), inv_sz);
		}
		fft(a, 1);
		a.resize(need);
		return a;
	}
	vector<T> square(vector<T> a) {
		return multiply(a, a, 1);
	}
};
//fast_ntt.cpp
/*
167772161; //= 2^25 * 5 + 1
469762049; //= 2^26 * 7 + 1
754974721; //= 2^24 * 45 + 1
1045430273; //= 2^20 * 997 + 1
1051721729; //= 2^20 * 1003 + 1
1053818881; //= 2^20 * 1005 + 1
*/

template<class T, const T md>
vector<T> anyntt(vector<T>&a, vector<T>&b, int eq = 0) {
	//今回は2^20以下だからこっちの方が速い (らしい) (:maroon_kansha:)
	const T m1 = 167772161, m2 = 469762049, m3 = 754974721;
	ntt<T, m1>n1;
	ntt<T, m2>n2;
	ntt<T, m3>n3;
	ntt<T, md>nn;
	auto a1  = n1.multiply(a, b, eq);
	auto a2 = n2.multiply(a, b, eq);
	auto a3  = n3.multiply(a, b, eq);
	const int n = a1.size();
	vector<T>ret(n);
	vector<T>m; m = {m1, m2, m3};
	vector<T>r; r = {1, n2.inv(m1), n3.inv(n3.mul(m1, m2))};
	T mm = nn.mul(m1, m2);
	rep(i, n){
		n2.add(a2[i], n2.sub2(m2, a1[i]));
		T v1 = n2.mul(a2[i], r[1]);
		n3.add(a3[i], n3.sub2(m3, n3.add2(a1[i], n3.mul(m1, v1))));
		T v2 = n3.mul(a3[i], r[2]);
		nn.add(a1[i], nn.add2(nn.mul(m1, v1), nn.mul(mm, v2)));
		ret[i] = a1[i];
	}
	return ret;
}


ll dp[64][25005], m;
int n, a[55];
ll way[25005];

int main(){
	ios::sync_with_stdio(0);
	
	cin >> n >> m;
	way[0] = 1;
	rep(i, n){
	    cin >> a[i];
	    for(int x=25003-a[i];x>=0;x--){
	        way[x+a[i]] += way[x];
	        if(way[x+a[i]] >= mod) way[x+a[i]] -= mod;
	    }
	}
	dp[0][0] = 1;
	rep(i, 60){
		vector<ll>A, B;
		rep(j, 25005) A.pb(dp[i][j]);
		rep(j, 25005) B.pb(way[j]);
		auto C = anyntt<ll,mod>(A, B);
		int f = ((m>>i)&1);
		rep(j, C.size()){
		    if(f != (j&1)) continue;
			dp[i+1][j/2] += C[j];
			if(dp[i+1][j/2] >= mod) dp[i+1][j/2] -= mod;
		}
	}
	cout << dp[60][0] << '\n';
}
0