結果

問題 No.1481 Rotation ABC
ユーザー SSRS
提出日時 2021-04-16 20:45:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 1,429 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 176,556 KB
実行使用メモリ 11,084 KB
最終ジャッジ日時 2024-07-02 23:16:21
合計ジャッジ時間 3,727 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long modpow(long long a, long long b){
	long long ans = 1;
	while (b > 0){
		if (b % 2 == 1){
			ans *= a;
			ans %= MOD;
		}
		a *= a;
		a %= MOD;
		b /= 2;
	}
	return ans;
}
long long modinv(long long a){
	return modpow(a, MOD - 2);
}
vector<long long> mf = {1};
long long modfact(int n){
  for (int i = mf.size(); i <= n; i++){
    long long next = mf.back() * i % MOD;
    mf.push_back(next);
  }
  return mf[n];
}
bool is_subseq(string S, string T){
  int N = S.size();
  int M = T.size();
  vector<vector<bool>> dp(N + 1, vector<bool>(M + 1, false));
  dp[0][0] = true;
  for (int i = 0; i < N; i++){
    for (int j = 0; j <= M; j++){
      if (dp[i][j]){
        dp[i + 1][j] = true;
        if (j < M){
          if (S[i] == T[j]){
            dp[i + 1][j + 1] = true;
          }
        }
      }
    }
  }
  return dp[N][M];
}
int main(){
  int N;
  cin >> N;
  string S;
  cin >> S;
  if (!is_subseq(S, "ABC") && !is_subseq(S, "BCA") && !is_subseq(S, "CAB")){
    cout << 1 << endl;
  } else {
    int A = 0, B = 0, C = 0;
    for (int i = 0; i < N; i++){
      if (S[i] == 'A'){
        A++;
      }
      if (S[i] == 'B'){
        B++;
      }
      if (S[i] == 'C'){
        C++;
      }
    }
    cout << (modfact(N) * modinv(modfact(A)) % MOD * modinv(modfact(B)) % MOD * modinv(modfact(C)) % MOD + MOD - N) % MOD << endl;
  }
}
0