結果

問題 No.2356 Back Door Tour in Four Seasons
ユーザー kumakumakumakuma
提出日時 2023-05-14 05:23:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 350 ms / 2,000 ms
コード長 2,837 bytes
コンパイル時間 3,235 ms
コンパイル使用メモリ 212,444 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2023-08-19 22:44:25
合計ジャッジ時間 11,402 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 245 ms
5,592 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 298 ms
5,752 KB
testcase_09 AC 349 ms
5,616 KB
testcase_10 AC 350 ms
5,908 KB
testcase_11 AC 350 ms
5,888 KB
testcase_12 AC 349 ms
5,672 KB
testcase_13 AC 350 ms
5,612 KB
testcase_14 AC 350 ms
5,900 KB
testcase_15 AC 175 ms
4,376 KB
testcase_16 AC 326 ms
5,716 KB
testcase_17 AC 332 ms
5,656 KB
testcase_18 AC 319 ms
5,784 KB
testcase_19 AC 180 ms
4,384 KB
testcase_20 AC 313 ms
5,676 KB
testcase_21 AC 256 ms
5,204 KB
testcase_22 AC 177 ms
4,684 KB
testcase_23 AC 128 ms
4,380 KB
testcase_24 AC 333 ms
5,708 KB
testcase_25 AC 10 ms
4,376 KB
testcase_26 AC 256 ms
4,808 KB
testcase_27 AC 331 ms
5,964 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 37 ms
7,716 KB
testcase_31 AC 37 ms
7,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/modint>
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define INF 2000000000000000000
#define ll long long
#define ull unsigned long long
#define ld long double
#define pll pair<ll, ll>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const double PI = 3.141592653589793238462643383279;

//UFWPの表記に違和感がある
//それ以降は訪れた街にある扉のうちどれか一つを用いて->今要る街
//扉の行先の組合せは全部で (N−1) A all通りあります->ほんと?
//扉の行き先を決められる事が明記されてなくて読みづらい
//扉1と扉2は区別するのか

ll mod = 998244353;
map<pll, ll> mp;
//=============modpow============================
ll modpow(ll a, ll n)
{
    if (mp.find({a, n}) != mp.end()) {
      return mp[{a, n}];
    }
    ll res = 1;
    while (n > 0)
    {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    mp[{a, n}] = res;
    return res;
}
//=================================================

//=============modinv============================
ll modinv(ll a, ll m = mod) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
//=================================================


int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<vector<ll>> num(4);
  ll N;
  cin >> N;
  ll sum = 0;
  for (ll i = 0; i < N; ++i) {
    char S;
    ll A;
    cin >> S >> A;
    sum += A;
    if (S == 'U') {
      num.at(0).push_back(A);
    }
    else if (S == 'F') {
      num.at(1).push_back(A);
    }
    else if (S == 'W') {
      num.at(2).push_back(A);
    }
    else {
      num.at(3).push_back(A);
    }
  }
  ll ans = 0;
  ll all = modpow(N - 1, sum);
  ll A = 0, B = 0, C = 0;
  for (ll i = 0; i < num.at(0).size(); ++i) {
    ll a = (modinv(modpow(N - 1, num.at(0).at(i))) % mod) * (modpow(N - 1, num.at(0).at(i)) - modpow(N - 2, num.at(0).at(i)) + mod) % mod;
    A += a;
    A %= mod;
  }
  for (ll j = 0; j < num.at(1).size(); ++j) {
    ll b = (modinv(modpow(N - 1, num.at(1).at(j))) % mod) * (modpow(N - 1, num.at(1).at(j)) - modpow(N - 2, num.at(1).at(j)) + mod) % mod;
    B += b;
    B %= mod;
  }
  for (ll k = 0; k < num.at(2).size(); ++k) {
    ll c = (modinv(modpow(N - 1, num.at(2).at(k))) % mod) * (modpow(N - 1, num.at(2).at(k)) - modpow(N - 2, num.at(2).at(k)) + mod) % mod;
    C += c;
    C %= mod;
  }

  ans += (((all * A % mod) * B % mod) * C  % mod) * (ll)num.at(3).size() % mod;
  ans %= mod;
  cout << ans << "\n";
}
0