QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#88787 | #5519. Count Hamiltonian Cycles | lmeowdn | RE | 2ms | 3460kb | C++14 | 1.4kb | 2023-03-17 13:47:44 | 2023-03-17 13:47:46 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define fi first
#define se second
#define eb emplace_back
#define popc __builtin_popcount
#define clz __builtin_clz
#define ctz __builtin_ctz
#define sgn(x) ((x)&1?-1:1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef unsigned long long ull;
typedef long double ld;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+c-'0'; c=getchar();}
return x*w;
}
const int N=1e6+9,mod=998244353,inv2=(mod+1)/2;
int n,ca,cb,f[N];
char s[N];
signed main() {
int T=read();
while(T--) {
scanf("%lld%s",&n,s+1);
f[1]=1; int ca=(s[1]=='W'), cb=(s[1]=='B');
rep(i,2,2*n) {
if(ca==cb) f[i]=f[i-1];
else if(s[i]=='W'&&ca>cb) f[i]=f[i-1];
else if(s[i]=='B'&&ca<cb) f[i]=f[i-1];
else if(s[i]=='W'&&ca+1==cb) f[i]=f[i-1]*2%mod;
else if(s[i]=='B'&&cb+1==ca) f[i]=f[i-1]*2%mod;
else if(s[i]=='W') f[i]=f[i-1]*(cb-ca)*(cb-ca-1)%mod;
else if(s[i]=='B') f[i]=f[i-1]*(ca-cb)*(ca-cb-1)%mod;
ca+=(s[i]=='W'), cb+=(s[i]=='B');
}
printf("%lld\n",f[2*n]*inv2%mod*inv2%mod);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3460kb
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...