結果
問題 | No.2874 Gunegune Tree |
ユーザー | Today03 |
提出日時 | 2024-09-06 23:15:02 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 446 ms / 2,000 ms |
コード長 | 3,612 bytes |
コンパイル時間 | 2,950 ms |
コンパイル使用メモリ | 252,728 KB |
実行使用メモリ | 31,292 KB |
最終ジャッジ日時 | 2024-09-06 23:15:21 |
合計ジャッジ時間 | 9,534 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,948 KB |
testcase_03 | AC | 33 ms
6,940 KB |
testcase_04 | AC | 190 ms
14,848 KB |
testcase_05 | AC | 70 ms
7,424 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 441 ms
29,476 KB |
testcase_08 | AC | 188 ms
15,744 KB |
testcase_09 | AC | 19 ms
6,944 KB |
testcase_10 | AC | 332 ms
25,520 KB |
testcase_11 | AC | 273 ms
20,864 KB |
testcase_12 | AC | 105 ms
8,576 KB |
testcase_13 | AC | 404 ms
29,432 KB |
testcase_14 | AC | 336 ms
24,728 KB |
testcase_15 | AC | 58 ms
6,940 KB |
testcase_16 | AC | 446 ms
30,484 KB |
testcase_17 | AC | 47 ms
6,940 KB |
testcase_18 | AC | 52 ms
6,940 KB |
testcase_19 | AC | 33 ms
6,940 KB |
testcase_20 | AC | 46 ms
6,944 KB |
testcase_21 | AC | 136 ms
12,032 KB |
testcase_22 | AC | 213 ms
16,384 KB |
testcase_23 | AC | 127 ms
11,520 KB |
testcase_24 | AC | 172 ms
14,208 KB |
testcase_25 | AC | 325 ms
22,052 KB |
testcase_26 | AC | 175 ms
14,848 KB |
testcase_27 | AC | 403 ms
29,980 KB |
testcase_28 | AC | 91 ms
9,344 KB |
testcase_29 | AC | 236 ms
18,800 KB |
testcase_30 | AC | 211 ms
15,488 KB |
testcase_31 | AC | 140 ms
12,416 KB |
testcase_32 | AC | 418 ms
31,292 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; /* dp[i][j]:=i日たったときに、向きがj(上、右上、左上、右、左)である確率 dp2[i][j]:=i日たったときに、向きがj(上、右上、左上、右、左)のときの期待値 */ template <ll MOD> struct ModInt { ll value; ModInt(ll x = 0) { if (x >= 0) { value = x % MOD; } else { value = MOD - (-x) % MOD; } } ModInt operator-() const { return ModInt(-value); } ModInt operator+() const { return ModInt(*this); } ModInt &operator+=(const ModInt &other) { value += other.value; if (value >= MOD) { value -= MOD; } return *this; } ModInt &operator-=(const ModInt &other) { value += MOD - other.value; if (value >= MOD) { value -= MOD; } return *this; } ModInt &operator*=(const ModInt other) { value = value * other.value % MOD; return *this; } ModInt &operator/=(ModInt other) { (*this) *= other.inv(); return *this; } ModInt operator+(const ModInt &other) const { return ModInt(*this) += other; } ModInt operator-(const ModInt &other) const { return ModInt(*this) -= other; } ModInt operator*(const ModInt &other) const { return ModInt(*this) *= other; } ModInt operator/(const ModInt &other) const { return ModInt(*this) /= other; } ModInt pow(ll x) const { ModInt ret(1), mul(value); while (x) { if (x & 1) { ret *= mul; } mul *= mul; x >>= 1; } return ret; } ModInt inv() const { return pow(MOD - 2); } bool operator==(const ModInt &other) const { return value == other.value; } bool operator!=(const ModInt &other) const { return value != other.value; } friend ostream &operator<<(ostream &os, const ModInt &x) { return os << x.value; } friend istream &operator>>(istream &is, ModInt &x) { ll v; is >> v; x = ModInt<MOD>(v); return is; } static constexpr ll get_mod() { return MOD; } }; using Mod998 = ModInt<998244353>; using Mod107 = ModInt<1000000007>; using mint = Mod998; int main() { int N; cin >> N; mint inv2 = mint(1) / 2, inv3 = mint(1) / 3; vector<vector<mint>> dp(N + 1, vector<mint>(5)); dp[1][0] = dp[1][1] = dp[1][2] = dp[1][3] = dp[1][4] = mint(1) / 5; for (int i = 1; i < N; i++) { for (int j = 0; j < 5; j++) { mint c = j == 0 || j == 4 ? inv2 : inv3; if (j > 0) dp[i + 1][j - 1] += dp[i][j] * c; dp[i + 1][j] += dp[i][j] * c; if (j < 4) dp[i + 1][j + 1] += dp[i][j] * c; } } vector<vector<mint>> dp2(N + 1, vector<mint>(5)); dp2[1][1] = dp2[1][2] = dp2[1][3] = mint(1) / 5; for (int i = 2; i <= N; i++) { for (int j = 0; j < 5; j++) { for (int k = -1; k <= 1; k++) { int pj = k + j; if (pj < 0 || pj >= 5) continue; mint c = pj == 0 || pj == 4 ? inv2 : inv3; mint up = j == 0 || j == 4 ? 0 : 1; dp2[i][j] += (dp2[i - 1][pj] / dp[i - 1][pj] + up) * dp[i - 1][pj] * c; } } } mint ans = 0; for (int i = 0; i < 5; i++) ans += dp2[N][i]; cout << ans << endl; }