結果

問題 No.1481 Rotation ABC
ユーザー SSRSSSRS
提出日時 2021-04-16 20:45:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,429 bytes
コンパイル時間 1,642 ms
コンパイル使用メモリ 175,356 KB
実行使用メモリ 11,060 KB
最終ジャッジ日時 2023-09-15 21:50:03
合計ジャッジ時間 4,229 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 13 ms
8,516 KB
testcase_14 AC 14 ms
9,080 KB
testcase_15 AC 13 ms
8,584 KB
testcase_16 AC 11 ms
7,552 KB
testcase_17 AC 11 ms
7,272 KB
testcase_18 AC 15 ms
9,532 KB
testcase_19 AC 11 ms
7,008 KB
testcase_20 AC 10 ms
7,048 KB
testcase_21 AC 14 ms
9,072 KB
testcase_22 AC 10 ms
6,720 KB
testcase_23 AC 17 ms
10,396 KB
testcase_24 AC 17 ms
10,352 KB
testcase_25 AC 16 ms
10,632 KB
testcase_26 AC 17 ms
10,316 KB
testcase_27 AC 17 ms
10,372 KB
testcase_28 AC 32 ms
10,868 KB
testcase_29 AC 32 ms
11,028 KB
testcase_30 AC 30 ms
10,876 KB
testcase_31 AC 31 ms
10,780 KB
testcase_32 AC 30 ms
10,828 KB
testcase_33 AC 30 ms
10,792 KB
testcase_34 AC 30 ms
10,820 KB
testcase_35 AC 31 ms
10,880 KB
testcase_36 AC 30 ms
11,036 KB
testcase_37 AC 30 ms
11,008 KB
testcase_38 AC 30 ms
11,060 KB
testcase_39 AC 30 ms
10,804 KB
testcase_40 AC 31 ms
10,872 KB
権限があれば一括ダウンロードができます

ソースコード

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