結果

問題 No.1481 Rotation ABC
ユーザー 👑 NachiaNachia
提出日時 2021-04-16 20:32:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,240 bytes
コンパイル時間 732 ms
コンパイル使用メモリ 78,864 KB
実行使用メモリ 5,740 KB
最終ジャッジ日時 2023-09-15 21:25:59
合計ジャッジ時間 2,434 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 7 ms
5,076 KB
testcase_14 AC 7 ms
5,356 KB
testcase_15 AC 7 ms
5,144 KB
testcase_16 AC 6 ms
4,776 KB
testcase_17 AC 6 ms
4,692 KB
testcase_18 AC 8 ms
5,464 KB
testcase_19 AC 6 ms
4,756 KB
testcase_20 AC 5 ms
4,628 KB
testcase_21 AC 8 ms
5,368 KB
testcase_22 AC 6 ms
4,488 KB
testcase_23 AC 8 ms
5,700 KB
testcase_24 AC 8 ms
5,740 KB
testcase_25 AC 8 ms
5,620 KB
testcase_26 AC 8 ms
5,668 KB
testcase_27 AC 8 ms
5,636 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 4 ms
4,380 KB
testcase_33 AC 4 ms
4,380 KB
testcase_34 AC 4 ms
4,384 KB
testcase_35 AC 4 ms
4,384 KB
testcase_36 AC 4 ms
4,380 KB
testcase_37 AC 4 ms
4,376 KB
testcase_38 AC 4 ms
4,380 KB
testcase_39 AC 4 ms
4,380 KB
testcase_40 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const ull M = 998244353;
vector<ull> F,I,iF;

ull powm(ull a,ull i){
  if(i==0) return 1;
  ull res = powm(a*a%M,i/2);
  if(i%2==1) res = res * a % M;
  return res;
}

void initComb(int Z){
  F.resize(Z+1);
  F[0]=1;
  for(int i=1; i<=Z; i++) F[i]=F[i-1]*i%M;
  I.resize(Z+1);
  I[1]=1;
  for(int i=2; i<=Z; i++) I[i]=M-M/i*I[M%i]%M;
  iF.resize(Z+1);
  iF[0]=1;
  for(int i=1; i<=Z; i++) iF[i]=iF[i-1]*I[i]%M;
}

ull Comb(int n,int r){ return F[n]*iF[r]%M*iF[n-r]%M; }

int main(){
  int N;
  string S;
  cin >> N >> S;
  
  string chs = "ABC";
  vector<int> Cchs(chs.size(),0);
  for(char c:S) rep(t,chs.size()) if(chs[t]==c) Cchs[t]++;

  for(char c:chs) if(S.find(c)==string::npos){ cout<<"1\n"; return 0; }

  {
    auto p = S.find("AC");
    if(p != string::npos){
      S = S.substr(p+1) + S.substr(0,p+1);
    }
  }

  bool fixed = true;
  rep(i,S.size()-1) if(S[i]<S[i+1]) fixed=false;
  if(fixed){ cout<<"1\n"; return 0; }

  initComb(N);
  ull ans = F[N];
  rep(t,3) ans = ans * iF[Cchs[t]] % M;
  ans = (ans + M - N) %M;
  cout << ans << "\n";

  return 0;
}
0