結果

問題 No.1481 Rotation ABC
ユーザー nok0
提出日時 2021-03-02 20:06:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 1,242 bytes
コンパイル時間 4,551 ms
コンパイル使用メモリ 252,500 KB
最終ジャッジ日時 2025-01-19 09:27:09
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

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