結果

問題 No.2356 Back Door Tour in Four Seasons
ユーザー momoyuumomoyuu
提出日時 2023-07-12 15:30:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 2,710 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 140,944 KB
実行使用メモリ 4,900 KB
最終ジャッジ日時 2023-10-12 05:47:01
合計ジャッジ時間 8,843 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 167 ms
4,356 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 174 ms
4,644 KB
testcase_09 AC 188 ms
4,612 KB
testcase_10 AC 187 ms
4,660 KB
testcase_11 AC 187 ms
4,608 KB
testcase_12 AC 188 ms
4,664 KB
testcase_13 AC 188 ms
4,640 KB
testcase_14 AC 188 ms
4,640 KB
testcase_15 AC 94 ms
4,352 KB
testcase_16 AC 173 ms
4,352 KB
testcase_17 AC 178 ms
4,456 KB
testcase_18 AC 170 ms
4,452 KB
testcase_19 AC 95 ms
4,352 KB
testcase_20 AC 168 ms
4,352 KB
testcase_21 AC 136 ms
4,352 KB
testcase_22 AC 95 ms
4,348 KB
testcase_23 AC 69 ms
4,352 KB
testcase_24 AC 177 ms
4,348 KB
testcase_25 AC 6 ms
4,348 KB
testcase_26 AC 136 ms
4,352 KB
testcase_27 AC 177 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 148 ms
4,352 KB
testcase_31 AC 149 ms
4,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<cassert>
#include<cmath>
#include<iomanip>
#include<map>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
using ll = long long;


const ll mod = 998'244'353;
//const ll mod = 1'000'000'007;
//const ll mod = 67'280'421'310'721;
struct mint{
    long long x;
    mint(long long x=0):x((x%mod+mod)%mod){}
    mint operator-() const{
        return mint(-x);
    }
    mint& operator+=(const mint& a){
        if((x+=a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator-=(const mint& a){
        if((x+=mod-a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator*=(const  mint& a){
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const{
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const{
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const{
        mint res(*this);
        return res*=a;
    }
    mint pow(long long n) const {
        assert(0 <= n);
        mint a = *this, r = 1;
        while (n) {
            if (n & 1) r *= a;
            a *= a;
            n >>= 1;
        }
        return r;
    }
    mint inv() const{
        return pow(mod-2);
    }
    mint& operator/=(const mint& a){
        return (*this)*=a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    bool operator==(const mint& a) const {
        return x == a.x;
    }
    bool operator<(const mint& a) const{
        return x < a.x;
    }
};

int main(){
    int n;
    cin>>n;
    vector<char> s(n);
    vector<int> a(n);
    for(int i = 0;i<n;i++) cin>>s[i]>>a[i];
    vector<mint> tmp(4,0);
    vector<ll> cnt(4,0);
    for(int i = 0;i<n;i++){
        if(s[i]=='U') {cnt[0] += a[i];}
        else if(s[i]=='F') cnt[1] += a[i];
        else if(s[i]=='W') cnt[2] += a[i];
        else cnt[3] += a[i];
    }

    for(int i = 0;i<n;i++){
        int ni = 0;
        if(s[i]=='U'){
            ni = 0;
        }else if(s[i]=='F'){
            ni = 1;
        }else if(s[i]=='W') {
            ni = 2;
        }else{
            ni = 3;
        }
        if(ni==3){
            tmp[ni] += mint(n-1).pow(cnt[ni]);
            continue;
        }
        mint other = mint(n-1).pow(cnt[ni]-a[i]);
        mint now = mint(n-1).pow(a[i]) - mint(n-2).pow(a[i]);
        tmp[ni] += now * other;
    }
    mint ans = 1;
    for(int i = 0;i<4;i++) ans *= tmp[i];
    cout<<ans<<endl;
            
}
0