QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#252878 | #6323. Range NEQ | std_abs# | AC ✓ | 948ms | 88824kb | C++14 | 3.8kb | 2023-11-16 14:12:55 | 2023-11-16 14:12:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 998244353, N = 1 << 21, G = 3;
ll add(ll a, ll b) {
a += b;
if (a >= mod) {
a -= mod;
}
return a;
}
ll sub(ll a, ll b) {
a -= b;
if (a < 0) {
a += mod;
}
return a;
}
ll mul(ll a, ll b) {
return a * b % mod;
}
ll mpow(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1, a = mul(a, a)) {
if (b & 1) {
ans = mul(ans, a);
}
}
return ans;
}
struct NTT {
ll w[N];
NTT() {
ll dw = mpow(G, (mod - 1) / N);
w[0] = 1;
for (int i = 1; i < N; ++i) {
w[i] = mul(w[i - 1], dw);
}
}
void operator () (vector <ll> &a, bool inv = false) {
int x = 0, n = a.size();
for (int j = 1; j < n - 1; ++j) {
for (int k = n >> 1; (x ^= k) < k; k >>= 1);
if (j < x) {
swap(a[x], a[j]);
}
}
for (int L = 2; L <= n; L <<= 1) {
int dx = N / L, dl = L >> 1;
for (int i = 0; i < n; i += L) {
for (int j = i, x = 0; j < i + dl; ++j, x += dx) {
ll tmp = mul(a[j + dl], w[x]);
a[j + dl] = sub(a[j], tmp);
a[j] = add(a[j], tmp);
}
}
}
if (inv) {
reverse(1 + all(a));
ll invn = mpow(n, mod - 2);
for (int i = 0; i < n; ++i) a[i] = mul(a[i], invn);
}
}
} ntt;
vector <ll> Mul(vector <ll> a, vector <ll> b, int bound = N) {
int m = a.size() + b.size() - 1, n = 1;
while (n < m) n <<= 1;
a.resize(n), b.resize(n);
ntt(a), ntt(b);
vector <ll> out(n);
for (int i = 0; i < n; ++i) out[i] = mul(a[i], b[i]);
ntt(out, true), out.resize(min(m, bound));
return out;
}
// vector <ll> Ln(vector <ll> a) {
// int n = a.size();
// if (n == 1) return {0};
// vector <ll> d
// }
// vector <ll> Pow(vector <ll> a, ll k) {
// int n = a.size(), m = 0;
// vector <ll> ans(n, 0);
// while (m < n && a[m] == 0) m++;
// if (k && m && (k >= n || k * m >= n)) return ans;
// if (m == n) return ans[0] = 1, ans;
// ll lead = m * k;
// vector <ll> b(a.begin() + m, a.end());
// ll base = mpow(b[0], k), inv = mpow(b[0], mod - 2);
// for (int i = 0; i < n - m; ++i) {
// b[i] = mul(b[i], inv);
// }
// b = Ln(b);
// for (int i = 0; i < n - m; ++i) {
// b[i] = mul(b[i], k % mod);
// }
// b = Exp(b);
// for (int i = lead; i < n; ++i) {
// ans[i] = mul(b[i - lead], base);
// }
// return ans;
// }
vector <ll> mmpow(vector <ll> a, ll k) {
if (k == 1) {
return a;
} else {
vector <ll> res = mmpow(a, k / 2);
res = Mul(res, res);
if (k & 1) {
res = Mul(res, a);
}
return res;
}
}
ll fac[N], facp[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
fac[0] = facp[0] = 1;
for (int i = 1; i < N; ++i) {
fac[i] = mul(fac[i - 1], i);
facp[i] = mpow(fac[i], mod - 2);
}
int n, m;
cin >> n >> m;
vector <ll> a(m + 1);
for (int i = 0; i <= m; ++i) {
a[i] = fac[m] * facp[i] % mod * facp[m - i] % mod * facp[m - i] % mod;
}
vector <ll> ans = mmpow(a, n);
ll res = 0;
for (int i = 0; i <= n * m; ++i) {
if (i & 1) {
res = sub(res, mul(ans[i], fac[n * m - i]));
} else {
res = add(res, mul(ans[i], fac[n * m - i]));
}
}
res = mul(res, mpow(fac[m], n));
cout << res << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 272ms
memory: 52552kb
input:
2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 281ms
memory: 52584kb
input:
5 1
output:
44
result:
ok 1 number(s): "44"
Test #3:
score: 0
Accepted
time: 285ms
memory: 52916kb
input:
167 91
output:
284830080
result:
ok 1 number(s): "284830080"
Test #4:
score: 0
Accepted
time: 278ms
memory: 52584kb
input:
2 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 265ms
memory: 52636kb
input:
2 3
output:
36
result:
ok 1 number(s): "36"
Test #6:
score: 0
Accepted
time: 280ms
memory: 52552kb
input:
2 4
output:
576
result:
ok 1 number(s): "576"
Test #7:
score: 0
Accepted
time: 277ms
memory: 52520kb
input:
3 1
output:
2
result:
ok 1 number(s): "2"
Test #8:
score: 0
Accepted
time: 277ms
memory: 52604kb
input:
3 2
output:
80
result:
ok 1 number(s): "80"
Test #9:
score: 0
Accepted
time: 268ms
memory: 52548kb
input:
3 3
output:
12096
result:
ok 1 number(s): "12096"
Test #10:
score: 0
Accepted
time: 277ms
memory: 52532kb
input:
3 4
output:
4783104
result:
ok 1 number(s): "4783104"
Test #11:
score: 0
Accepted
time: 263ms
memory: 52528kb
input:
4 1
output:
9
result:
ok 1 number(s): "9"
Test #12:
score: 0
Accepted
time: 284ms
memory: 52544kb
input:
4 2
output:
4752
result:
ok 1 number(s): "4752"
Test #13:
score: 0
Accepted
time: 280ms
memory: 52528kb
input:
4 3
output:
17927568
result:
ok 1 number(s): "17927568"
Test #14:
score: 0
Accepted
time: 278ms
memory: 52556kb
input:
4 4
output:
776703752
result:
ok 1 number(s): "776703752"
Test #15:
score: 0
Accepted
time: 273ms
memory: 52584kb
input:
5 2
output:
440192
result:
ok 1 number(s): "440192"
Test #16:
score: 0
Accepted
time: 267ms
memory: 52548kb
input:
5 3
output:
189125068
result:
ok 1 number(s): "189125068"
Test #17:
score: 0
Accepted
time: 277ms
memory: 52528kb
input:
5 4
output:
975434093
result:
ok 1 number(s): "975434093"
Test #18:
score: 0
Accepted
time: 603ms
memory: 88700kb
input:
1000 1000
output:
720037464
result:
ok 1 number(s): "720037464"
Test #19:
score: 0
Accepted
time: 277ms
memory: 52728kb
input:
72 42
output:
638177567
result:
ok 1 number(s): "638177567"
Test #20:
score: 0
Accepted
time: 271ms
memory: 52612kb
input:
15 19
output:
663050288
result:
ok 1 number(s): "663050288"
Test #21:
score: 0
Accepted
time: 274ms
memory: 52656kb
input:
68 89
output:
94365047
result:
ok 1 number(s): "94365047"
Test #22:
score: 0
Accepted
time: 282ms
memory: 52668kb
input:
92 37
output:
652605307
result:
ok 1 number(s): "652605307"
Test #23:
score: 0
Accepted
time: 282ms
memory: 52676kb
input:
61 87
output:
498277867
result:
ok 1 number(s): "498277867"
Test #24:
score: 0
Accepted
time: 276ms
memory: 52744kb
input:
81 40
output:
133095344
result:
ok 1 number(s): "133095344"
Test #25:
score: 0
Accepted
time: 280ms
memory: 52628kb
input:
7 91
output:
524164813
result:
ok 1 number(s): "524164813"
Test #26:
score: 0
Accepted
time: 281ms
memory: 52648kb
input:
31 18
output:
361233485
result:
ok 1 number(s): "361233485"
Test #27:
score: 0
Accepted
time: 279ms
memory: 52712kb
input:
74 54
output:
500686087
result:
ok 1 number(s): "500686087"
Test #28:
score: 0
Accepted
time: 278ms
memory: 52528kb
input:
32 2
output:
586504335
result:
ok 1 number(s): "586504335"
Test #29:
score: 0
Accepted
time: 433ms
memory: 70204kb
input:
656 718
output:
346764298
result:
ok 1 number(s): "346764298"
Test #30:
score: 0
Accepted
time: 368ms
memory: 61016kb
input:
254 689
output:
358078813
result:
ok 1 number(s): "358078813"
Test #31:
score: 0
Accepted
time: 527ms
memory: 70348kb
input:
713 674
output:
914437613
result:
ok 1 number(s): "914437613"
Test #32:
score: 0
Accepted
time: 299ms
memory: 56636kb
input:
136 698
output:
56687290
result:
ok 1 number(s): "56687290"
Test #33:
score: 0
Accepted
time: 386ms
memory: 60540kb
input:
369 401
output:
312325811
result:
ok 1 number(s): "312325811"
Test #34:
score: 0
Accepted
time: 298ms
memory: 54072kb
input:
280 204
output:
280012063
result:
ok 1 number(s): "280012063"
Test #35:
score: 0
Accepted
time: 355ms
memory: 60972kb
input:
904 225
output:
162909174
result:
ok 1 number(s): "162909174"
Test #36:
score: 0
Accepted
time: 941ms
memory: 87960kb
input:
855 928
output:
39885159
result:
ok 1 number(s): "39885159"
Test #37:
score: 0
Accepted
time: 391ms
memory: 61224kb
input:
503 365
output:
745115888
result:
ok 1 number(s): "745115888"
Test #38:
score: 0
Accepted
time: 737ms
memory: 87512kb
input:
646 996
output:
610925577
result:
ok 1 number(s): "610925577"
Test #39:
score: 0
Accepted
time: 776ms
memory: 88528kb
input:
990 918
output:
203469632
result:
ok 1 number(s): "203469632"
Test #40:
score: 0
Accepted
time: 810ms
memory: 87884kb
input:
961 949
output:
169566857
result:
ok 1 number(s): "169566857"
Test #41:
score: 0
Accepted
time: 719ms
memory: 88324kb
input:
946 932
output:
352423195
result:
ok 1 number(s): "352423195"
Test #42:
score: 0
Accepted
time: 948ms
memory: 88560kb
input:
903 981
output:
196309824
result:
ok 1 number(s): "196309824"
Test #43:
score: 0
Accepted
time: 683ms
memory: 87972kb
input:
916 988
output:
487208972
result:
ok 1 number(s): "487208972"
Test #44:
score: 0
Accepted
time: 746ms
memory: 88660kb
input:
982 982
output:
387421488
result:
ok 1 number(s): "387421488"
Test #45:
score: 0
Accepted
time: 903ms
memory: 88440kb
input:
955 911
output:
955637031
result:
ok 1 number(s): "955637031"
Test #46:
score: 0
Accepted
time: 677ms
memory: 88492kb
input:
906 999
output:
798469943
result:
ok 1 number(s): "798469943"
Test #47:
score: 0
Accepted
time: 734ms
memory: 88824kb
input:
982 975
output:
193506289
result:
ok 1 number(s): "193506289"
Test #48:
score: 0
Accepted
time: 796ms
memory: 87888kb
input:
921 991
output:
431202149
result:
ok 1 number(s): "431202149"