結果

問題 No.1481 Rotation ABC
ユーザー nok0nok0
提出日時 2021-03-02 20:06:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 1,242 bytes
コンパイル時間 4,442 ms
コンパイル使用メモリ 257,984 KB
実行使用メモリ 38,848 KB
最終ジャッジ日時 2024-04-14 04:51:25
合計ジャッジ時間 8,792 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
38,476 KB
testcase_01 AC 65 ms
38,516 KB
testcase_02 AC 67 ms
38,432 KB
testcase_03 AC 71 ms
38,488 KB
testcase_04 AC 68 ms
38,372 KB
testcase_05 AC 75 ms
38,408 KB
testcase_06 AC 72 ms
38,508 KB
testcase_07 AC 74 ms
38,444 KB
testcase_08 AC 75 ms
38,448 KB
testcase_09 AC 68 ms
38,408 KB
testcase_10 AC 74 ms
38,488 KB
testcase_11 AC 72 ms
38,368 KB
testcase_12 AC 69 ms
38,344 KB
testcase_13 AC 79 ms
38,596 KB
testcase_14 AC 74 ms
38,700 KB
testcase_15 AC 73 ms
38,716 KB
testcase_16 AC 70 ms
38,592 KB
testcase_17 AC 73 ms
38,496 KB
testcase_18 AC 71 ms
38,696 KB
testcase_19 AC 76 ms
38,604 KB
testcase_20 AC 67 ms
38,612 KB
testcase_21 AC 75 ms
38,764 KB
testcase_22 AC 73 ms
38,576 KB
testcase_23 AC 75 ms
38,560 KB
testcase_24 AC 74 ms
38,764 KB
testcase_25 AC 70 ms
38,664 KB
testcase_26 AC 76 ms
38,700 KB
testcase_27 AC 76 ms
38,556 KB
testcase_28 AC 76 ms
38,712 KB
testcase_29 AC 73 ms
38,772 KB
testcase_30 AC 76 ms
38,696 KB
testcase_31 AC 70 ms
38,696 KB
testcase_32 AC 70 ms
38,628 KB
testcase_33 AC 72 ms
38,540 KB
testcase_34 AC 73 ms
38,848 KB
testcase_35 AC 76 ms
38,676 KB
testcase_36 AC 81 ms
38,736 KB
testcase_37 AC 75 ms
38,764 KB
testcase_38 AC 76 ms
38,744 KB
testcase_39 AC 74 ms
38,664 KB
testcase_40 AC 71 ms
38,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/all>
using namespace std;

using mint = atcoder::modint998244353;

const int CombMAX = 3000000;
std::vector<mint> fac(CombMAX + 1), finv(CombMAX + 1), inv(CombMAX + 1);

struct Combinationinit {
	Combinationinit() {
		const int MOD = mint::mod();
		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;
			inv[i] = MOD - inv[MOD % i] * (MOD / i);
			finv[i] = finv[i - 1] * inv[i];
		}
	}
} Combinationinit_;

int main() {
	int n;
	string s;
	cin >> n >> s;

	int count_a = count(s.begin(), s.end(), 'A');
	int count_b = count(s.begin(), s.end(), 'B');
	int count_c = count(s.begin(), s.end(), 'C');

	bool can_operation = 0;
	auto check = [&]() {
		bool exi_a = 0, exi_ab = 0;
		for(auto &c : s) {
			if(c == 'A') exi_a = 1;
			if(c == 'B' and exi_a) exi_ab = 1;
			if(c == 'C' and exi_ab) can_operation = 1;
		}
	};

	auto rot = [&]() {
		for(auto &c : s) {
			if(c != 'C')
				c++;
			else
				c = 'A';
		}
	};

	for(int i = 0; i < 3; i++) check(), rot();

	if(can_operation) {
		mint res = fac[n] * finv[count_a] * finv[count_b] * finv[count_c] - n;
		cout << res.val() << endl;
	} else
		cout << 1 << endl;

	return 0;
}
0