QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#225380 | #5519. Count Hamiltonian Cycles | piaoyun | RE | 0ms | 3732kb | C++20 | 1.1kb | 2023-10-24 16:28:12 | 2023-10-24 16:28:13 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define otto auto
const int MAXN=1e6+10;
const int INF=1ll*1e7*1e9;
const int MOD = 998244353;
const int inv = (MOD + 1) / 2;
int T,N,M,K,P,Q;
string str;
int a[MAXN];
int dp[MAXN];
void prepare(){
scanf("%lld",&N);
cin>>str;
dp[0] = 1;
for(int i = 1;i <= 2 * N; i++){
char c = str[i-1];
if(a[i-1] == 0){
dp[i] = dp[i-1];
}
else if(a[i-1] == 1){
if(c == 'B') dp[i] = dp[i-1] * 2 % MOD;
else dp[i] = dp[i-1];
}
else if(a[i-1] >= 2){
if(c == 'B') dp[i] = dp[i-1] * (a[i-1] - 1) % MOD * a[i-1] % MOD;
else dp[i] = dp[i-1];
}
else if(a[i-1] == -1){
if(c == 'W') dp[i] = dp[i-1] * 2 % MOD;
else dp[i] = dp[i-1];
}
else if(a[i-1] <= -2){
if(c == 'W') dp[i] = (a[i-1] + 1) * a[i-1] % MOD * dp[i-1] % MOD;
else dp[i] = dp[i-1];
}
if(c == 'W') a[i] = a[i-1] + 1;
else a[i] = a[i-1] - 1;
}
cout<<dp[2*N] * inv %MOD * inv% MOD<<endl;
}
signed main(){
//ios::sync_with_stdio(0);
T=1;
scanf("%lld",&T);
while(T--){
prepare();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3732kb
input:
3 2 WWBB 3 WBWBWB 7 WWWWBWBBWWBBBB
output:
1 2 62208
result:
ok 3 number(s): "1 2 62208"
Test #2:
score: -100
Runtime Error
input:
1 1000000 BWBWBBBWWWBWBBBWBBWWWBWBBWWBWBWWWBBBBWBBWWBWBBBWBBBWWBWWBBBBWWWWBWBBWWBBWWWWBBBBWWBWWWWBBBWWBWBWWWBWWBWWBWWBWWBWWWWBWBWWWWWWBWWBWWBWBWBWBWWWBWBBBWBBBWWWBBBWBBBWBBWBWWBBBWWBWBWWWBBBBWBBBWWWBWBBBWBBWWBWBWWBBBWWWWBBWBBBWBBBBBBWBWBWBBBWBBBBBWBBBWWBWBWWBBWWWWBBBBBBBBBWBWWBBBWBWWBBBWBBBBWWBWWBWW...