結果

問題 No.1481 Rotation ABC
ユーザー nok0nok0
提出日時 2021-02-08 20:25:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,226 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 205,588 KB
実行使用メモリ 6,224 KB
最終ジャッジ日時 2023-09-20 01:46:48
合計ジャッジ時間 3,809 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,980 KB
testcase_01 AC 4 ms
5,760 KB
testcase_02 AC 4 ms
5,704 KB
testcase_03 AC 4 ms
5,736 KB
testcase_04 AC 4 ms
5,744 KB
testcase_05 AC 4 ms
5,888 KB
testcase_06 AC 4 ms
5,708 KB
testcase_07 AC 4 ms
5,688 KB
testcase_08 AC 4 ms
5,708 KB
testcase_09 AC 4 ms
5,680 KB
testcase_10 AC 3 ms
5,760 KB
testcase_11 AC 4 ms
5,824 KB
testcase_12 AC 4 ms
5,696 KB
testcase_13 AC 8 ms
6,084 KB
testcase_14 AC 8 ms
6,224 KB
testcase_15 AC 7 ms
5,932 KB
testcase_16 AC 7 ms
5,876 KB
testcase_17 AC 7 ms
5,952 KB
testcase_18 AC 8 ms
5,960 KB
testcase_19 AC 7 ms
5,880 KB
testcase_20 AC 7 ms
5,856 KB
testcase_21 AC 8 ms
5,940 KB
testcase_22 AC 6 ms
5,864 KB
testcase_23 AC 9 ms
5,932 KB
testcase_24 AC 8 ms
5,968 KB
testcase_25 AC 9 ms
5,916 KB
testcase_26 AC 9 ms
5,908 KB
testcase_27 AC 8 ms
5,924 KB
testcase_28 AC 6 ms
5,956 KB
testcase_29 AC 6 ms
5,900 KB
testcase_30 AC 6 ms
5,816 KB
testcase_31 AC 8 ms
5,988 KB
testcase_32 AC 7 ms
6,048 KB
testcase_33 AC 8 ms
5,908 KB
testcase_34 AC 8 ms
5,976 KB
testcase_35 AC 8 ms
5,968 KB
testcase_36 AC 7 ms
5,960 KB
testcase_37 AC 7 ms
6,048 KB
testcase_38 AC 8 ms
5,988 KB
testcase_39 AC 8 ms
5,912 KB
testcase_40 AC 7 ms
6,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int CombMAX = 100010, mod = 998244353;
long long fac[CombMAX + 1], finv[CombMAX + 1], inv[CombMAX + 1];

struct Combinationinit {
	Combinationinit() {
		fac[0] = fac[1] = 1;
		finv[0] = finv[1] = 1;
		inv[1] = 1;
		for(int i = 2; i <= CombMAX; i++) {
			fac[i] = (fac[i - 1] * i) % mod;
			inv[i] = (mod - (inv[mod % i] * (mod / i)) % mod) % mod;
			finv[i] = (finv[i - 1] * inv[i]) % mod;
		}
	}
} Combinationinit_;

int n, ac, bc, cc;
string s, u;
int main() {
	cin >> n >> s;
	ac = count(s.begin(), s.end(), 'A');
	bc = count(s.begin(), s.end(), 'B');
	cc = count(s.begin(), s.end(), 'C');
	//A,B,C全てが存在しない場合
	if(min({ac, bc, cc}) == 0) {
		cout << "1\n";
		return 0;
	}

	int cp = 0;
	for(int i = 0; i < n; i++) {
		if(s[i] == 'C' and !i) cp = 0;
		if(i and s[i - 1] == 'A' and s[i] == 'C') cp = i;
	}

	rotate(s.begin(), s.begin() + cp, s.end());
	string u = s;
	sort(u.rbegin(), u.rend());
	//C..CB..BA..Aであり、操作が不可能な場合
	if(s == u) {
		cout << "1\n";
		return 0;
	}

	long long res = ((((fac[n] * finv[ac]) % mod * finv[bc]) % mod * finv[cc]) % mod - n) % mod;
	if(res < 0) res += mod;

	cout << res << '\n';

	return 0;
}
0