提出 #73275734


ソースコード 拡げる

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll mod = 998244353;

ll modpow(ll a, ll b, ll m) {
	ll p = 1, q = a;
	for (int i = 0; i < 30; i++) {
		if ((b >> i) & 1) {
			p *= q;
			p %= m;
		}
		q *= q; q %= m;
	}
	return p;
}

ll divs(ll a, ll b, ll m) {
	return (a * modpow(b, m - 2, m)) % m;
}

ll ncr(ll n, ll r, vector<ll>& fact, vector<ll>& inv) {
	if (n < r || r < 0) return 0;
	return (fact[n] * inv[r] % mod) * inv[n - r] % mod;
}

ll narabekae(ll a, ll b, ll c, vector<ll> &fact, vector<ll> &inv) {
	return (fact[a + b + c] * inv[a] % mod) * (inv[b] * inv[c] % mod) % mod;
}

int main() {
	// step 1. input
	ll a, b, x, c; cin >> a >> b >> x >> c;

	// step 2. initialize
	vector<ll> fact(a + b + c + 1, 0);
	vector<ll> inv(a + b + c + 1, 0);
	fact[0] = 1;
	for (int i = 1; i <= a + b + c; i++) fact[i] = (1LL * i * fact[i - 1]) % mod;
	for (int i = 0; i <= a + b + c; i++) inv[i] = divs(1, fact[i], mod);

	// step 3. solve
	ll threshold = (2 * a + b) / (c + 1);
	ll sum = 0;
	for (int i = 0; i <= a; i++) {
		ll v = (threshold - 2 * i);
		for (int j = max(0LL, v - 1); j <= min(b, v + 2); j++) {
			ll score = 2 * i + j;
			if (score <= threshold) continue;

			// first case
			if (j >= 1 && score - 1 <= threshold) {
				ll way1 = narabekae(i, j - 1, 0, fact, inv);
				ll way2 = narabekae(a - i, b - j, c, fact, inv);
				ll way3 = narabekae(a, b, c, fact, inv);
				ll prob = divs(way1 * way2 % mod, way3, mod);
				sum += prob * score;
			}

			// second case
			if (i >= 1 && score - 2 <= threshold) {
				ll way1 = narabekae(i - 1, j, 0, fact, inv);
				ll way2 = narabekae(a - i, b - j, c, fact, inv);
				ll way3 = narabekae(a, b, c, fact, inv);
				ll prob = divs(way1 * way2 % mod, way3, mod);
				sum += prob * score;
			}
			sum %= mod;
		}
	}

	// step 4. output
	cout << sum << endl;
	return 0;
}

提出情報

提出日時
問題 K - Keep or Gamble
ユーザ E869120
言語 C++23 (GCC 15.2.0)
得点 100
コード長 1941 Byte
結果 AC
実行時間 431 ms
メモリ 50360 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 100 / 100
結果
AC × 3
AC × 40
セット名 テストケース
Sample 00_sample_00, 00_sample_01, 00_sample_02
All 00_sample_00, 00_sample_01, 00_sample_02, 01_small_00, 01_small_01, 01_small_02, 01_small_03, 01_small_04, 02_medium_00, 02_medium_01, 02_medium_02, 02_medium_03, 02_medium_04, 03_large_00, 03_large_01, 03_large_02, 03_large_03, 03_large_04, 04_ut_large_c_small_00, 04_ut_large_c_small_01, 04_ut_large_c_small_02, 04_ut_large_c_small_03, 04_ut_large_c_small_04, 05_ut_large_c_medium_00, 05_ut_large_c_medium_01, 05_ut_large_c_medium_02, 05_ut_large_c_medium_03, 05_ut_large_c_medium_04, 06_hand_00, 06_hand_01, 06_hand_02, 06_hand_03, 06_hand_04, 06_hand_05, 06_hand_06, 06_hand_07, 06_hand_08, 06_hand_09, 06_hand_10, 06_hand_11
ケース名 結果 実行時間 メモリ
00_sample_00 AC 1 ms 3628 KiB
00_sample_01 AC 1 ms 3540 KiB
00_sample_02 AC 1 ms 3608 KiB
01_small_00 AC 1 ms 3668 KiB
01_small_01 AC 1 ms 3660 KiB
01_small_02 AC 1 ms 3608 KiB
01_small_03 AC 1 ms 3624 KiB
01_small_04 AC 1 ms 3652 KiB
02_medium_00 AC 72 ms 12668 KiB
02_medium_01 AC 40 ms 8416 KiB
02_medium_02 AC 35 ms 7888 KiB
02_medium_03 AC 57 ms 10720 KiB
02_medium_04 AC 70 ms 12412 KiB
03_large_00 AC 304 ms 43064 KiB
03_large_01 AC 301 ms 42208 KiB
03_large_02 AC 176 ms 26320 KiB
03_large_03 AC 255 ms 36536 KiB
03_large_04 AC 291 ms 41184 KiB
04_ut_large_c_small_00 AC 142 ms 20964 KiB
04_ut_large_c_small_01 AC 196 ms 27616 KiB
04_ut_large_c_small_02 AC 212 ms 30304 KiB
04_ut_large_c_small_03 AC 219 ms 22648 KiB
04_ut_large_c_small_04 AC 208 ms 29724 KiB
05_ut_large_c_medium_00 AC 195 ms 28640 KiB
05_ut_large_c_medium_01 AC 163 ms 24416 KiB
05_ut_large_c_medium_02 AC 174 ms 26048 KiB
05_ut_large_c_medium_03 AC 164 ms 24508 KiB
05_ut_large_c_medium_04 AC 199 ms 29280 KiB
06_hand_00 AC 360 ms 50340 KiB
06_hand_01 AC 361 ms 50148 KiB
06_hand_02 AC 360 ms 50340 KiB
06_hand_03 AC 360 ms 50360 KiB
06_hand_04 AC 431 ms 34556 KiB
06_hand_05 AC 431 ms 34492 KiB
06_hand_06 AC 431 ms 34528 KiB
06_hand_07 AC 431 ms 34528 KiB
06_hand_08 AC 431 ms 34528 KiB
06_hand_09 AC 431 ms 34528 KiB
06_hand_10 AC 121 ms 18892 KiB
06_hand_11 AC 120 ms 19000 KiB