QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#465217#8836. Highway Hoaxucup-team2172#WA 1280ms73904kbC++146.9kb2024-07-06 18:54:312024-07-06 18:54:32

Judging History

你现在查看的是最新测评结果

  • [2024-07-06 18:54:32]
  • 评测
  • 测评结果:WA
  • 用时:1280ms
  • 内存:73904kb
  • [2024-07-06 18:54:31]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;
const int base = 2;
const int g = 3;
const int INF = 998244353;

inline int qpow(int x, int y){
	int ret = 1;
	for(; y; y >>= 1){
		if(y & 1)
			ret = 1ll * ret * x % INF;
		x = 1ll * x * x % INF;
	}
	return ret;
}

namespace POLY{
	const int SIZ = 1048576;
	int A[SIZ], B[SIZ], C[SIZ], T[SIZ], W[SIZ], IW[SIZ], btf[SIZ];
	int fac[SIZ], ifac[SIZ], iv[SIZ];
	
	inline void init(int siz){
		for(int i = 0; i < siz; ++i) btf[i] = (btf[i >> 1] >> 1) + (i & 1) * (siz >> 1);
		int cur = qpow(g, (INF - 1) / siz);
		W[0] = 1; for(int i = 1; i < siz; ++i) W[i] = 1ll * W[i - 1] * cur % INF;
		cur = qpow(g, INF - 1 - (INF - 1) / siz);
		IW[0] = 1; for(int i = 1; i < siz; ++i) IW[i] = 1ll * IW[i - 1] * cur % INF;
		memset(A, 0, siz << 2);
		memset(B, 0, siz << 2);
		return; 
	}
	
	inline void ntt(int *vec, int siz, int INTT = 1){
		for(int i = 0; i < siz; ++i) if(i < btf[i])
			swap(vec[i], vec[btf[i]]);
		for(int step = 1; step < siz; step <<= 1){
			for(int i = 0; i < step; ++i)
				T[i] = (~INTT ? W[siz / (step << 1) * i] : IW[siz / (step << 1) * i]);
			for(int i = 0; i < siz; i += step << 1){
				int *p1 = vec + i, *p2 = vec + i + step, *p3 = T;
				for(int j = 0; j < step; ++j, ++p1, ++p2, ++p3){
					int u = *p1, v = 1ll * (*p3) * (*p2) % INF;
					*p1 = (u + v) - (u + v >= INF ? INF : 0);
					*p2 = (u - v) + (u - v < 0 ? INF : 0);
				}
			}
		}
		if(!~INTT){
			int inv = qpow(siz, INF - 2);
			for(int i = 0; i < siz; ++i)
				vec[i] = 1ll * vec[i] * inv % INF;
		}
		return;
	}
	
	class Poly : public vector<int>{
	public:
		inline Poly(int n_ = 1){ clear(); resize(n_, 0); return; }
		
		inline Poly &operator *= (const Poly &O){
			int siz = 1;
			for(; siz < size() + O.size() - 1; siz <<= 1);
			if(1ll * size() * O.size() <= 4096){
				memset(C, 0, siz << 2);
				for(int i = 0; i < size(); ++i) for(int j = 0; j < O.size(); ++j)
					(C[i + j] += 1ll * at(i) * O[j] % INF) %= INF;
				resize(siz);
				for(int i = 0; i < siz; ++i)
					at(i) = C[i];
				return *this;
			}
			
			init(siz);
			for(int i = 0; i < size(); ++i) A[i] = at(i); ntt(A, siz);
			for(int i = 0; i < O.size(); ++i) B[i] = O[i]; ntt(B, siz);
			for(int i = 0; i < siz; ++i) C[i] = 1ll * A[i] * B[i] % INF;
			ntt(C, siz, -1);
			resize(siz); for(int i = 0; i < siz; ++i) at(i) = C[i];
			return *this;
		}
	
		inline Poly operator * (const Poly &O)const{ Poly R = *this; R *= O; return R; }
		inline Poly operator / (const Poly &O)const{ Poly R = *this * O.inv(O.size()); return R; } 
		inline Poly operator + (const Poly &O)const{
			Poly R(max(size(), O.size()));
			for(int i = 0; i < size(); ++i) R[i] = at(i);
			for(int i = 0; i < O.size(); ++i) (R[i] += O[i]) %= INF;
			return R; 
		}
		inline Poly operator - (const Poly &O)const{
			Poly R(max(size(), O.size()));
			for(int i = 0; i < size(); ++i) R[i] = at(i);
			for(int i = 0; i < O.size(); ++i) (R[i] += INF - O[i]) %= INF;
			return R;
		}
		
		inline Poly deri()const{ Poly R(size() - 1); for(int i = 1; i < size(); ++i) R[i - 1] = 1ll * at(i) * i % INF; return R; }
		inline Poly inte()const{ Poly R(size() + 1); for(int i = 0; i < size(); ++i) R[i + 1] = 1ll * at(i) * iv[i + 1] % INF; return R; }
		
		inline Poly inv()const{
			int n = size();
			if(n == 1){ Poly R(1); R[0] = qpow(at(0), INF - 2); return R; }
			int m = (n + 1) >> 1;
			Poly A = *this; A.resize(m);
			Poly B = A.inv();
			Poly R = *this;
			R *= B; R.resize(n);
			R *= B; R.resize(n);
			return B + B - R; 
		}
		
		inline Poly inv(int n)const{
				if(n == 1){
					Poly R(1);
					R[0] = qpow(at(0), INF - 2);
					return R;
				}
				
				int n0 = (n + 1) >> 1;
				Poly A = *this; A.resize(n0);
				Poly B = A.inv(n0);
				Poly R = (*this) * B; R.resize(n);
				R *= B; R.resize(n);
				return B + B - R;
		}
        
        inline Poly sqrt(){
    		if(size() == 1){ Poly R(1); R[0] = 1; return R; }
    		int m = (size() + 1) >> 1;
    		Poly A = *this; A.resize(m);
    		Poly f_0 = A.sqrt();
    		Poly f = f_0 - (f_0 * f_0 - *this) * (f_0 + f_0).inv();
    		f.resize(size());
    		return f;
  		}
	} ;
	
	inline Poly ln(const Poly &A){ return (A.deri() / A).inte(); }
	
	inline Poly exp(const Poly &T){
		int n = T.size();
		if(n == 1){ Poly R(1); R[0] = 1; return R; }
		int m = (n + 1) >> 1;
		Poly A = T; A.resize(m);
		Poly B = exp(A); B.resize(n);
		Poly R(1); R[0] = 1;
		R = B * (R + T - ln(B)); R.resize(n);
		return R;
	}
    
    inline void polyMod(Poly A, Poly B, Poly &C, Poly &D){
  		int n = A.size(), m = B.size();
  		Poly A_ = A, B_ = B;
  		reverse(A_.begin(), A_.end()); A_.resize(n - m + 1);
  		reverse(B_.begin(), B_.end()); B_.resize(n - m + 1);
  		C = A_ * (B_.inv()); C.resize(n - m + 1); reverse(C.begin(), C.end());
  		D = A - B * C;
  		return;
	}
}

inline POLY::Poly cdq(vector<POLY::Poly> &vec, int l, int r) {
	if (l == r) return vec[l];
	int md = l + r >> 1;
	return cdq(vec, l, md) * cdq(vec, md + 1, r);
}

int n;
int f[maxn][5];
vector<pair<int, int> > tree[maxn];
char s[maxn];

inline void dfs(int u, int p) {
	int dir = 0;
	int shift = 0;
	vector<POLY::Poly> vec;
	for (auto e: tree[u]) {
		int v = e.first;
		if (v != p){
			dfs(v, u);
			POLY::Poly Q(3);
			Q[0] = f[v][-1 + base], Q[1] = f[v][0 + base], Q[2] = f[v][1 + base];
			vec.push_back(Q);
			++shift;
		} else dir = e.second;
	}
	
	POLY::Poly P;
	if (vec.size()) P = cdq(vec, 0, (int)vec.size() - 1);
	else P = POLY::Poly(3), shift = 1, P[0 + shift] = 1;
	int g[5] = {(shift - 2 >= 0 ? P[shift - 2]: 0), P[shift - 1], P[shift], P[shift + 1], (shift + 2 < P.size() ? P[shift + 2] : 0)};
	/*
	printf("vec size = %d\n", vec.size());
	for (auto Q: vec) {
		for (auto x: Q) printf("%d ", x); puts("");
	}
	for (auto x: P) printf("%d ", x); puts("");
	*/
	
	int have = (s[u] == 'S');
	
	if (dir == 0) {
		if (have) f[u][0 + base] = (g[0 + base] + g[-1 + base]) % INF;
		else f[u][0 + base] = (g[0 + base] + g[1 + base]) % INF;
	}
	else if (dir == -1) {
		if (have) {
			f[u][0 + base] = (g[0 + base] + g[-1 + base]) % INF;
			f[u][-1 + base] = (g[-1 + base] + g[-2 + base]) % INF;
		} else {
			f[u][-1 + base] = (g[-1 + base] + g[0 + base]) % INF;
			f[u][0 + base] = g[0 + base];
		}
	}
	else {
		if (have) {
			f[u][1 + base] = (g[0 + base] + g[1 + base]) % INF;
			f[u][0 + base] = (g[0 + base] + g[-1 + base]) % INF;
		}
		else {
			f[u][0 + base] = (g[0 + base] + g[1 + base]) % INF;
			f[u][1 + base] = (g[1 + base] + g[2 + base]) % INF;
		}
	}
	
	// printf("u = %d f = (%d %d %d)\n", u, f[u][-1 + base], f[u][0 + base], f[u][1 + base]);
}

int main(){
	scanf("%d", &n);
	scanf("%s", s);
	for (int i = 1; i < n; ++i) {
		int u, v; scanf("%d%d", &u, &v);
		--u, --v;
		tree[u].push_back(make_pair(v, 1));
		tree[v].push_back(make_pair(u, -1));
	}
	
	dfs(0, -1);
	
	printf("%d\n", f[0][0 + base]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 12844kb

input:

5
SFSFS
2 1
2 3
4 3
4 5

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 12712kb

input:

4
SFFF
1 2
1 3
1 4

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 3ms
memory: 13276kb

input:

7
SSSSFFF
1 2
3 2
4 3
4 5
4 6
2 7

output:

13

result:

ok 1 number(s): "13"

Test #4:

score: 0
Accepted
time: 0ms
memory: 11924kb

input:

2
FS
1 2

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 2ms
memory: 10732kb

input:

3
FFF
3 1
3 2

output:

1

result:

ok 1 number(s): "1"

Test #6:

score: 0
Accepted
time: 0ms
memory: 14516kb

input:

4
FFFF
1 2
4 2
2 3

output:

1

result:

ok 1 number(s): "1"

Test #7:

score: 0
Accepted
time: 3ms
memory: 14100kb

input:

5
FFFFF
4 2
2 1
3 1
3 5

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 0ms
memory: 14432kb

input:

6
FSSSSF
5 3
2 5
1 2
2 6
4 2

output:

3

result:

ok 1 number(s): "3"

Test #9:

score: 0
Accepted
time: 1092ms
memory: 73824kb

input:

199995
FSSFFFSSSFSFFSSSFSSSSSFSFSSSFSFFSSFSFSFFFSSSFSSFSFFSFFFFSSFFSSSFFSFSFFFFFFFFFFFFFSSSFSSFFFSSSFSFSSSFFFSFFSFSSSSFSFSFSFSSFFSFSFFSFFSSFSSSFSFFSSSFFSFFFSFFSFSFSFSFSSSFSSSSFFSSFFFFFSFSSSFFSSSSSSSSFFFSFSFFSSSFSFFFFFSFFFFSSSSSSSFFFSFFSFSSSFSFSFFFFSFSSFSSFFFSSFFFSSFSFFFSSSFSFSSFFSFSFFSFFSSFSFSSSSFFF...

output:

233157276

result:

ok 1 number(s): "233157276"

Test #10:

score: 0
Accepted
time: 1082ms
memory: 73056kb

input:

199996
FSSFFSFSSSFFFFFFSFSSFSFFSSFFFFFFFSSSFFSFSSFSSFSFSSFSSSFFFFSFFFFFFSSSSFFFSFSSFSSFFSSFSFFSSSSSFSFFFFSFSFFSFSSFSFSSSSSFSFSSSFFSSFSSFSFFSFFSSFSFSSFSSSFSSSFFSSFFFFSFFSFSFSFFFSFSSSFFSFFSSSSSSSSFSSFSSFSSSSFSSSFSSSFSFFFFFFSFFSSSFSSFFFSFSFSFSSSFSFFSSSSFSSFSFSSFSSFSFFFSSSFFSSFSFSFFSSSFFFFSSSSSSSFSSSFFF...

output:

509139031

result:

ok 1 number(s): "509139031"

Test #11:

score: 0
Accepted
time: 1072ms
memory: 73100kb

input:

199997
FFSSSFSSFSFSFSSSSFSFFSSFFFSSFFSSSSSSFSSSFFSSSFFFSFFSFSFSSSFFSSSFSFFFFFSSFFSSFFFFSSSFFFFSSSSFFFSSFFSSSFSFFSFSFSFSFFSSFSFSSSFSSFSSFSFFSSFFFSFSSFSFFSSSFSFFSFFFFSSFFSFSFFFSFSFFSFSFSFFSSFFFSSFSFFSFSSSSSFFFFFSSSSSSSSSFFFFSSFFFSSFFSFSFSSSFSFSFFSSSSFSFSFFFSSFSSFFSFFSSFSSSSSSSFSSSFSSSFSFSSSSFSFFSSSFSS...

output:

917209642

result:

ok 1 number(s): "917209642"

Test #12:

score: 0
Accepted
time: 1079ms
memory: 72684kb

input:

199998
FFFSSSSSFSSSFFFFFFSSSSFSSFFFSFSSFFFSFFFFSFFFFFFFSSSFFSFSFFSFSFSFFSSFSFFSFSFSFSFFSSFFSFFFSFSFSSSSFSSSFFFSSSSFFSFFFFSFFFFFFSFSSFSFSSFFSSFSFFSFSSFSFSSFSSSSFSSFFSSFSSSSSSSSFFSFFSSSSSSSSSSSSFFSSFFFFFFSSFFSFFFFSSFFFSFFSSFFFFFFFFSFSSFSSSSFFSFSFFSSSSFSFSFFFSSFSFFFSFFSSFFSSSSSSSFSSSFFFSSFSSSSSFFFFSFSF...

output:

722227522

result:

ok 1 number(s): "722227522"

Test #13:

score: 0
Accepted
time: 1085ms
memory: 73904kb

input:

199999
FSFSFSFSSFSSSFSSFFSFSFFFFFFSSFSSSFSFSSFFSSSFSSSFSSSFSFFSFSFFFFFFSFFFSFSSSSSSFFSFFSFFFFFFFSSSFFFSFFSSSFSFSSSSSFSSFSFSFFSFSFFSSFSFSSFFSFSSSSSSSSSSSFFSSSSSFSFFFSFFSFFFSFFFFFSFFFFSSSSSSSFFFFSFFSSSFSFSSFFFFSSFSFSFSSSFSSSSFSSFSFFSFFFSSFSSFFFSFSFFFSFFFFSFSFSSSSSSSFFSSFFFSSFSSSSSFSSSFFFFFSFSSSFFFSFFS...

output:

22915769

result:

ok 1 number(s): "22915769"

Test #14:

score: 0
Accepted
time: 1067ms
memory: 72992kb

input:

200000
SFSSSSFSFFSFFFSFSFSSSSSSSSFFSSSFFSFFSSFFFFFFFFFFSSFFFFFFFSFFSFFFFFSSSFFFSFFSFFFFFSSFFSFFFFSSSFSFFSFSSFSFFFFFFFSFFSSFSSFSSFFSSFSSSSSFSSSSFFSFFFFFSSFSFFSFFFSSSSFFSFFFFFSSSSSSFFFSFFSFFFSFFFFFFSSSFSFFFFFSFSSSFFSSSFSFSFSSSFFFFFSSFFFSFSFFFSFSSSSSFSFFSFSSFFSSFSFFFFFSSFSFSFFFSSSFSFFFFSSSSFFFSSFFSFSFS...

output:

246807982

result:

ok 1 number(s): "246807982"

Test #15:

score: 0
Accepted
time: 1280ms
memory: 64340kb

input:

199980
SFSFFSSSSSFSSSFFFSSSSFFSSFSSSSFFFFSSFFSSFFFFSFSSFFSSSSSFFFSSSSFFFFFFFSFFSFSSSFSFFFFSFSFFSFSFFSFSFSFSFFSSSSFFSSFSSFFFFSFSFFFFFSSSFSFFFFFFSFFSSSFFSSSFFSSSFSFSFSSSFSSSFSSSSSFSFFFSSSFSSSFFSFSFSFFFSSSSFSSSFSSFSSSFFFFFSFFSFFFFSSSFFSSFFSSSFSSSFSSSFSSFSFSSFSFSFFSSSSFSSFSSSSSSSFSSSSSSSFSSSSSFFSFFFSFFS...

output:

854698427

result:

ok 1 number(s): "854698427"

Test #16:

score: -100
Wrong Answer
time: 975ms
memory: 62904kb

input:

199981
SFFSSFSSFSSSFSSSFSSFSFSSFFFFFFFFSFFFFFSFFFSSFSFSFFSSFSFFFFFSFFFFSSSFFSSFSSFSSSFFSFFSSSFFSSSFSFSSFFFSSFFFSSSSFFSFSFFSFSSFSFSFSFSSSSFFSSSSFSSFSSSSFSFSSSFSFSFFFSSSFSFSFFFFSSSSSSFFSSFSSFSFSFFSFFSSSSSSFFSFFSFFSSFFSFSFSFSFSSSFFSFFFSSFFSSFFFFSSFSSFSSSSSFFSSSFFSSFFSFFFSSSSSFSSFFSFSFFSFFSFSSSFFFSFSSSS...

output:

87594914

result:

wrong answer 1st numbers differ - expected: '990783292', found: '87594914'