結果

問題 No.1481 Rotation ABC
ユーザー chocoruskchocorusk
提出日時 2021-04-16 20:54:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 2,594 bytes
コンパイル時間 3,404 ms
コンパイル使用メモリ 188,788 KB
実行使用メモリ 19,432 KB
最終ジャッジ日時 2023-09-15 22:15:39
合計ジャッジ時間 5,392 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
19,036 KB
testcase_01 AC 7 ms
19,056 KB
testcase_02 AC 7 ms
19,036 KB
testcase_03 AC 7 ms
18,976 KB
testcase_04 AC 7 ms
19,016 KB
testcase_05 AC 7 ms
19,016 KB
testcase_06 AC 7 ms
18,976 KB
testcase_07 AC 7 ms
19,044 KB
testcase_08 AC 7 ms
19,060 KB
testcase_09 AC 7 ms
19,056 KB
testcase_10 AC 7 ms
19,024 KB
testcase_11 AC 7 ms
19,040 KB
testcase_12 AC 7 ms
19,300 KB
testcase_13 AC 10 ms
19,300 KB
testcase_14 AC 10 ms
19,168 KB
testcase_15 AC 9 ms
19,112 KB
testcase_16 AC 9 ms
19,136 KB
testcase_17 AC 9 ms
19,100 KB
testcase_18 AC 10 ms
19,216 KB
testcase_19 AC 9 ms
19,112 KB
testcase_20 AC 9 ms
19,168 KB
testcase_21 AC 10 ms
19,136 KB
testcase_22 AC 9 ms
19,116 KB
testcase_23 AC 11 ms
19,156 KB
testcase_24 AC 11 ms
19,400 KB
testcase_25 AC 10 ms
19,156 KB
testcase_26 AC 11 ms
19,192 KB
testcase_27 AC 11 ms
19,156 KB
testcase_28 AC 11 ms
19,164 KB
testcase_29 AC 11 ms
19,188 KB
testcase_30 AC 10 ms
19,312 KB
testcase_31 AC 10 ms
19,236 KB
testcase_32 AC 10 ms
19,432 KB
testcase_33 AC 10 ms
19,148 KB
testcase_34 AC 11 ms
19,136 KB
testcase_35 AC 10 ms
19,156 KB
testcase_36 AC 11 ms
19,168 KB
testcase_37 AC 10 ms
19,292 KB
testcase_38 AC 11 ms
19,308 KB
testcase_39 AC 10 ms
19,392 KB
testcase_40 AC 10 ms
19,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
using mint=modint998244353;
mint f[2000010], invf[2000010];
void fac(int n){
    f[0]=1;
    for(ll i=1; i<=n; i++) f[i]=f[i-1]*i;
    invf[n]=f[n].inv();
    for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1);
}
mint comb(int x, int y){
    if(!(0<=y && y<=x)) return 0;
    return f[x]*invf[y]*invf[x-y];
}
int main()
{
    int n; cin>>n;
    string s; cin>>s;
    // set<string> st;
    // queue<string> que;
    // que.push(s);
    // st.insert(s);
    // while(!que.empty()){
    //     auto t=que.front(); que.pop();
    //     for(int i=0; i<n; i++){
    //         if(t[i]!='A') continue;
    //         for(int j=0; j<n; j++){
    //             if(t[j]!='B') continue;
    //             for(int k=0; k<n; k++){
    //                 if(t[k]!='C') continue;
    //                 if(!((i<j && j<k) || (j<k && k<i) || (k<i && i<j))) continue;
    //                 string u=t;
    //                 u[i]='B', u[j]='C', u[k]='A';
    //                 if(st.find(u)==st.end()){
    //                     st.insert(u);
    //                     que.push(u);
    //                 }
    //             }
    //         }
    //     }
    // }
    // cout<<st.size()<<endl;
    fac(n);
    int ca=0, cb=0, cc=0;
    for(int i=0; i<n; i++){
        if(s[i]=='A') ca++;
        else if(s[i]=='B') cb++;
        else cc++;
    }
    mint ans=f[n]*invf[ca]*invf[cb]*invf[cc];
    ans-=mint(n);
    bool ok=0;
    int z=0;
    for(int i=0; i<n; i++){
        if(z==0 && s[i]=='A') z++;
        if(z==1 && s[i]=='B') z++;
        if(z==2 && s[i]=='C') z++;
    }
    if(z==3) ok=1;
    z=0;
    for(int i=0; i<n; i++){
        if(z==0 && s[i]=='B') z++;
        if(z==1 && s[i]=='C') z++;
        if(z==2 && s[i]=='A') z++;
    }
    if(z==3) ok=1;
    z=0;
    for(int i=0; i<n; i++){
        if(z==0 && s[i]=='C') z++;
        if(z==1 && s[i]=='A') z++;
        if(z==2 && s[i]=='B') z++;
    }
    if(z==3) ok=1;
    if(ok) cout<<ans.val()<<endl;
    else cout<<1<<endl;
    return 0;
}
0