結果

問題 No.2356 Back Door Tour in Four Seasons
ユーザー AngrySadEight
提出日時 2023-04-26 16:43:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 657 ms / 2,000 ms
コード長 1,581 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 77,224 KB
最終ジャッジ日時 2025-02-12 14:11:47
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
typedef long long ll;

ll mod2 = 998244353;

ll my_pow(ll x, ll n, ll mod){
    // 繰り返し二乗法.x^nをmodで割った余り.
    ll ret;
    if (n == 0){
        ret = 1;
    }
    else if (n % 2 == 1){
        ret = (x * my_pow((x * x) % mod, n / 2, mod)) % mod;
    }
    else{
        ret = my_pow((x * x) % mod, n / 2, mod);
    }
    return ret;
}

ll inv(ll x, ll mod){
    return my_pow(x, mod - 2, mod);
}

int main(){
    ll N;
    cin >> N;
    assert((2 <= N && N <= 300000));
    vector<char> S(N);
    vector<ll> A(N);
    ll all_sum = 0;
    ll spring = 0;
    for (ll i = 0; i < N; i++){
        cin >> S[i] >> A[i];
        assert((S[i] == 'U' || S[i] == 'F' || S[i] == 'W' || S[i] == 'P'));
        assert((1 <= A[i] && A[i] <= 100000));
        all_sum += A[i];
        if (S[i] == 'P') spring++;
    }
    ll summer = 0;
    ll fall = 0;
    ll winter = 0;
    for (ll i = 0; i < N; i++){
        ll prd = my_pow(N - 1, A[i], mod2);
        prd = (prd + mod2 - my_pow(N - 2, A[i], mod2)) % mod2;
        prd = (prd * inv(my_pow(N - 1, A[i], mod2) , mod2)) % mod2;
        if (S[i] == 'U') summer = (summer + prd) % mod2;
        if (S[i] == 'F') fall = (fall + prd) % mod2;
        if (S[i] == 'W') winter = (winter + prd) % mod2;
    }
    ll ans = 1;
    ans = (ans * summer) % mod2;
    ans = (ans * fall) % mod2;
    ans = (ans * winter) % mod2;
    ans = (ans * spring) % mod2;
    ans = (ans * my_pow(N - 1, all_sum, mod2)) % mod2;
    cout << ans << endl;
    
}
0