結果
| 問題 |
No.2356 Back Door Tour in Four Seasons
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-14 05:23:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 355 ms / 2,000 ms |
| コード長 | 2,837 bytes |
| コンパイル時間 | 2,141 ms |
| コンパイル使用メモリ | 206,168 KB |
| 最終ジャッジ日時 | 2025-02-13 00:26:09 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 28 |
ソースコード
#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";
}