結果
| 問題 |
No.1481 Rotation ABC
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-04-16 20:32:46 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 13 ms / 2,000 ms |
| コード長 | 1,240 bytes |
| コンパイル時間 | 1,227 ms |
| コンパイル使用メモリ | 77,564 KB |
| 最終ジャッジ日時 | 2025-01-20 18:51:54 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 38 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
const ull M = 998244353;
vector<ull> F,I,iF;
ull powm(ull a,ull i){
if(i==0) return 1;
ull res = powm(a*a%M,i/2);
if(i%2==1) res = res * a % M;
return res;
}
void initComb(int Z){
F.resize(Z+1);
F[0]=1;
for(int i=1; i<=Z; i++) F[i]=F[i-1]*i%M;
I.resize(Z+1);
I[1]=1;
for(int i=2; i<=Z; i++) I[i]=M-M/i*I[M%i]%M;
iF.resize(Z+1);
iF[0]=1;
for(int i=1; i<=Z; i++) iF[i]=iF[i-1]*I[i]%M;
}
ull Comb(int n,int r){ return F[n]*iF[r]%M*iF[n-r]%M; }
int main(){
int N;
string S;
cin >> N >> S;
string chs = "ABC";
vector<int> Cchs(chs.size(),0);
for(char c:S) rep(t,chs.size()) if(chs[t]==c) Cchs[t]++;
for(char c:chs) if(S.find(c)==string::npos){ cout<<"1\n"; return 0; }
{
auto p = S.find("AC");
if(p != string::npos){
S = S.substr(p+1) + S.substr(0,p+1);
}
}
bool fixed = true;
rep(i,S.size()-1) if(S[i]<S[i+1]) fixed=false;
if(fixed){ cout<<"1\n"; return 0; }
initComb(N);
ull ans = F[N];
rep(t,3) ans = ans * iF[Cchs[t]] % M;
ans = (ans + M - N) %M;
cout << ans << "\n";
return 0;
}
Nachia