QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#388964#1262. Justice For EveryoneoscaryangAC ✓1148ms9992kbC++173.4kb2024-04-13 22:09:592024-04-13 22:10:00

Judging History

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

  • [2024-04-13 22:10:00]
  • 评测
  • 测评结果:AC
  • 用时:1148ms
  • 内存:9992kb
  • [2024-04-13 22:09:59]
  • 提交

answer

#include<bits/stdc++.h>

#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define lep(i, a, b) for(int i = (a); i >= (b); --i)

using namespace std;

mt19937 gen(time(0));

inline int read() {
	int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x; 
}

const int N = 35, M = 205, MOD = 998244353;

// CALC
inline void inc (int &x, int y) { x += y - MOD; x += (x >> 31) & MOD; }
inline int qpow (int a, int b) { int res = 1; for(; b; b >>= 1, a = 1ll * a * a % MOD) if(b & 1) res = 1ll * res * a % MOD; return res; }
inline int qinv (int a) { return qpow(a, MOD - 2); }

struct mint {
	int x; 
	mint (int ix = 0) { x = ix % MOD; if(x < 0) x += MOD; }
	mint operator += (mint a) { inc (x, a.x); return a; }
	mint operator -= (mint a) { inc (x, MOD - a.x); return a; }
	mint operator *= (mint a) { x = 1ll * x * a.x % MOD; return a; }
	mint friend operator + (mint a, mint b) { a += b; return a; }
	mint friend operator - (mint a, mint b) { a -= b; return a; }
	mint friend operator * (mint a, mint b) { a *= b; return a; }
} fc[N * M], ifc[N * M];

inline void init (int n) {
	fc[0] = ifc[0] = 1; 
	rep (i, 1, n) fc[i] = i * fc[i - 1];
	ifc[n] = qinv (fc[n].x);
	lep (i, n - 1, 1) ifc[i] = (i + 1) * ifc[i + 1];
}

int n, m, tot, a[N], b[N];
mint G[M][M], vG[M][N * M]; // poly
mint A[N][N]; // det
mint f[N * M], pf[N * M], g[N * M]; // val
mint ans;

inline mint det (mint a[][N], int n) {
	mint res = 1, coef = 1; int t; 
	rep (i, 1, n) rep (j, i + 1, n) {
		while (a[i][i].x) {
			t = a[j][i].x / a[i][i].x;
			rep (k, 1, n) a[j][k] -= a[i][k] * t;
			swap (a[i], a[j]); coef *= -1;
		}
		swap (a[i], a[j]); coef *= -1;
	}
	rep (i, 1, n) res *= a[i][i]; 
	// cout << res.x << endl;
	return res * coef;
}

inline void lagrange(int n, mint *x, mint *y, mint *g) {
	rep(i, 0, n) g[i] = 0; 
	vc<mint> tmp(n + 2), tmp2(n + 2);
	tmp[0] = 1;
	rep(i, 0, n) {
		tmp[i + 1] = 0;
		lep(j, i + 1, 0) tmp[j] = (j ? tmp[j - 1] : 0) - x[i] * tmp[j];
	}
	rep(i, 0, n) {
		mint co = 1; 
		rep(j, 0, n) if(j != i) co *= x[i] - x[j];
		co = y[i] * qinv (co.x);
		tmp2[n] = 1;
		lep(j, n - 1, 0) tmp2[j] = tmp[j + 1] + x[i] * tmp2[j + 1];
		rep(j, 0, n) g[j] += co * tmp2[j];
	} 
}

signed main() {
	n = read ();
	rep (i, 1, n) a[i] = read ();
	rep (i, 1, n) b[i] = read (), tot += b[i] - a[i], m = max (m, b[i] + 1);
	
	if (tot & 1) return puts ("0"), 0;
	rep (i, 1, n) rep (j, 1, n) if ((a[i] < a[j]) != (b[i] < b[j])) return puts ("0"), 0;
	sort (a + 1, a + 1 + n); 
	sort (b + 1, b + 1 + n);
	
	init (n * m);
	
	// calc poly
	rep (i, 0, m) {
		rep (j, 0, i / 2) G[i][j] = ifc[j] * ifc[i - 2 * j];	
		rep (z, 0, n * m) {
			mint v = 1;
			for (int j = 0; j <= i / 2; j++, v *= z) 
				vG[i][z] += G[i][j] * v;
		}
	}
	
	// calc val
	rep (z, 0, n * m) {
		rep (i, 1, n) rep (j, 1, n) {
			int d = b[j] - a[i];
			A[i][j] = d < 0 ? 0 : vG[d][z];
		}
		pf[z] = z; f[z] = det (A, n);
	}
	
	// lagrange
	lagrange (n * m, pf, f, g);
	
	// calc ans
	mint coef = 1; tot /= 2;
	rep (X, 0, tot) 
		ans += coef * fc[tot] * ifc[tot - X] * fc[2 * tot - 2 * X] * g[X], coef *= -1;
	ans *= qinv (qpow (2, tot));
	cout << ans.x << endl;
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 9988kb

input:

3
1 2 3
3 4 5

output:

1

result:

ok answer is '1'

Test #2:

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

input:

3
1 2 3
7 8 9

output:

42

result:

ok answer is '42'

Test #3:

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

input:

3
1 4 7
3 6 9

output:

6

result:

ok answer is '6'

Test #4:

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

input:

1
1
3

output:

0

result:

ok answer is '0'

Test #5:

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

input:

1
1
1

output:

1

result:

ok answer is '1'

Test #6:

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

input:

2
1 4
4 7

output:

1

result:

ok answer is '1'

Test #7:

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

input:

10
10 9 8 7 6 1 2 3 4 5
11 12 13 114 115 120 129 128 127 126

output:

0

result:

ok answer is '0'

Test #8:

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

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 131

output:

0

result:

ok answer is '0'

Test #9:

score: 0
Accepted
time: 543ms
memory: 9688kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

output:

936606510

result:

ok answer is '936606510'

Test #10:

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

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199

output:

0

result:

ok answer is '0'

Test #11:

score: 0
Accepted
time: 1016ms
memory: 9836kb

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 200

output:

836228983

result:

ok answer is '836228983'

Test #12:

score: 0
Accepted
time: 37ms
memory: 9708kb

input:

10
10 9 8 7 6 5 4 3 2 1
110 109 108 107 106 105 104 103 102 101

output:

422463757

result:

ok answer is '422463757'

Test #13:

score: 0
Accepted
time: 1148ms
memory: 9740kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

output:

575061951

result:

ok answer is '575061951'