結果
問題 |
No.2874 Gunegune Tree
|
ユーザー |
![]() |
提出日時 | 2024-09-09 14:05:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 9 ms / 2,000 ms |
コード長 | 2,381 bytes |
コンパイル時間 | 646 ms |
コンパイル使用メモリ | 44,288 KB |
最終ジャッジ日時 | 2025-02-24 06:19:00 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:68:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 68 | scanf("%d", &n); | ~~~~~^~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*- * * 2874.cc: No.2874 Gunegune Tree - yukicoder */ #include<cstdio> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 200000; const int K = 5; const int MOD = 998244353; enum { R, RU, U, LU, L }; /* typedef */ template<const int MOD> struct MI { int v; MI(): v() {} MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; } MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } MI operator+(const MI m) const { return MI(v + m.v); } MI operator-(const MI m) const { return MI(v + MOD - m.v); } MI operator*(const MI m) const { return MI((long long)v * m.v); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } bool operator==(const MI m) const { return v == m.v; } bool operator!=(const MI m) const { return v != m.v; } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; using mi = MI<MOD>; /* global variables */ mi invs[K + 1], dp[MAX_N + 1][K]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); for (int i = 1; i <= K; i++) invs[i] = mi(i).inv(); for (int j = 0; j < K; j++) dp[1][j] = invs[K]; for (int i = 1; i < n; i++) { // R -> R, RU dp[i + 1][R] += dp[i][R] * invs[2]; dp[i + 1][RU] += dp[i][R] * invs[2]; // RU -> R, RU, U dp[i + 1][R] += dp[i][RU] * invs[3]; dp[i + 1][RU] += dp[i][RU] * invs[3]; dp[i + 1][U] += dp[i][RU] * invs[3]; // U -> RU, U, LU dp[i + 1][RU] += dp[i][U] * invs[3]; dp[i + 1][U] += dp[i][U] * invs[3]; dp[i + 1][LU] += dp[i][U] * invs[3]; // LU -> L, LU, U dp[i + 1][L] += dp[i][LU] * invs[3]; dp[i + 1][LU] += dp[i][LU] * invs[3]; dp[i + 1][U] += dp[i][LU] * invs[3]; // L -> L, LU dp[i + 1][L] += dp[i][L] * invs[2]; dp[i + 1][LU] += dp[i][L] * invs[2]; } mi sum = 0; for (int i = 1; i <= n; i++) sum += dp[i][RU] + dp[i][U] + dp[i][LU]; printf("%d\n", (int)sum); return 0; }