QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#97430 | #6323. Range NEQ | jeroenodb# | AC ✓ | 255ms | 31032kb | C++14 | 3.9kb | 2023-04-16 19:28:15 | 2023-04-16 19:28:19 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1<<20, oo = 1e9;
const long long MOD = 998244353;
class mint {
public:
int d;
mint () {d=0;}
mint (long long _d) : d(_d%MOD){};
mint operator*(const mint& o) const {
return ((ll)d*o.d)%MOD;
}
mint operator+(const mint& o) const {
long long tmp = d+o.d;
if(tmp>=MOD) tmp-=MOD;
return tmp;
}
mint operator-(const mint& o) const {
long long tmp = d-o.d;
if(tmp<0) tmp+=MOD;
return tmp;
}
mint operator^(long long b) const {
mint tmp = 1;
mint power = *this;
while(b) {
if(b&1) {
tmp = tmp*power;
}
power = power*power;
b/=2;
}
return tmp;
}
mint operator/(const mint& o) {
return *this * (o^(MOD-2));
}
bool operator==(const mint& o) {
return d==o.d;
}
friend mint& operator+=(mint& a, const mint& o) {
a.d+=o.d;
if(a.d>=MOD) a.d-=MOD;
return a;
}
};
typedef mint cd;
void revperm(cd* in, int n) {
for(int i=0,j=0;i<n;++i) {
if(i<j) swap(in[i],in[j]);
for(int k = n >> 1; (j ^= k) < k; k >>= 1);
}
}
cd w[mxN+1]; // stores w^j for each j in [0,n-1]
void precomp() {
w[0] = 1;
int pw = (MOD-1)/mxN;
w[1] = mint(3)^pw;
for(int i= 2;i<=mxN;++i) {
w[i] = w[i-1]*w[1];
}
}
void fft(cd* in, int n, bool reverse=false) {
int lg = __lg(n);
assert(1<<lg == n);
int stride = mxN/n;
revperm(in,n);
for(int s=1;s<=lg;++s) {
int pw = 1<<s;
int mstride = stride*(n>>s);
for(int j=0;j<n;j+=pw) {
// do FFT merging on out array
cd* even = in+j, *odd = in+j+pw/2;
for(int i=0;i<pw/2;++i) {
cd& power = w[reverse?mxN-mstride*i:mstride*i];
auto tmp = power*odd[i];
odd[i] = even[i] - tmp;
even[i] = even[i] + tmp;
}
}
}
if(reverse) {
mint fac = mint(1)/n;
for(int i=0;i<n;++i) in[i]=in[i]*fac;
}
}
int total;
vector<cd> polypow(vector<cd>& a, int len, int k) {
int ptwo=1;
while(ptwo<len) ptwo*=2;
a.resize(ptwo);
fft(a.data(),ptwo);
for(int i=0;i<ptwo;++i)
a[i] = a[i]^k;
fft(a.data(),ptwo,true);
return a;
}
const int mxF = 2e6+2;
mint fact[mxF], ifact[mxF];
mint ncr(int a, int b) {
if(b>a or a<0 or b<0) return 0;
return fact[a]*ifact[b]*ifact[a-b];
}
void precomp2() {
fact[0]=1;
for(int i=1;i<mxF;++i) {
fact[i]=fact[i-1]*i;
}
ifact[mxF-1] = mint(1)/fact[mxF-1];
for(int i=mxF-2;i>=0;--i) {
ifact[i]=ifact[i+1]*(i+1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
precomp();
precomp2();
int n,m; cin >> n >> m;
vector<cd> a(m+1);
a[0]=1;
for(int i=1;i<=m;++i) {
a[i] = a[i-1]*(m-i+1);
}
for(int i=1;i<=m;++i) {
a[i] =a[i]* ncr(m,i);
}
a = polypow(a,n*m+1,n);
mint ans=0;
mint sgn=1;
for(int k=0;k<=n*m;++k) {
ans+=fact[n*m-k]*sgn*a[k];
sgn=mint(0)-sgn;
}
cout << ans.d << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 21ms
memory: 23016kb
input:
2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 28ms
memory: 23128kb
input:
5 1
output:
44
result:
ok 1 number(s): "44"
Test #3:
score: 0
Accepted
time: 23ms
memory: 23140kb
input:
167 91
output:
284830080
result:
ok 1 number(s): "284830080"
Test #4:
score: 0
Accepted
time: 28ms
memory: 23032kb
input:
2 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 22ms
memory: 23032kb
input:
2 3
output:
36
result:
ok 1 number(s): "36"
Test #6:
score: 0
Accepted
time: 21ms
memory: 23024kb
input:
2 4
output:
576
result:
ok 1 number(s): "576"
Test #7:
score: 0
Accepted
time: 28ms
memory: 23108kb
input:
3 1
output:
2
result:
ok 1 number(s): "2"
Test #8:
score: 0
Accepted
time: 23ms
memory: 23032kb
input:
3 2
output:
80
result:
ok 1 number(s): "80"
Test #9:
score: 0
Accepted
time: 28ms
memory: 23024kb
input:
3 3
output:
12096
result:
ok 1 number(s): "12096"
Test #10:
score: 0
Accepted
time: 29ms
memory: 23124kb
input:
3 4
output:
4783104
result:
ok 1 number(s): "4783104"
Test #11:
score: 0
Accepted
time: 25ms
memory: 23136kb
input:
4 1
output:
9
result:
ok 1 number(s): "9"
Test #12:
score: 0
Accepted
time: 22ms
memory: 23024kb
input:
4 2
output:
4752
result:
ok 1 number(s): "4752"
Test #13:
score: 0
Accepted
time: 25ms
memory: 23020kb
input:
4 3
output:
17927568
result:
ok 1 number(s): "17927568"
Test #14:
score: 0
Accepted
time: 23ms
memory: 23024kb
input:
4 4
output:
776703752
result:
ok 1 number(s): "776703752"
Test #15:
score: 0
Accepted
time: 21ms
memory: 23068kb
input:
5 2
output:
440192
result:
ok 1 number(s): "440192"
Test #16:
score: 0
Accepted
time: 21ms
memory: 23024kb
input:
5 3
output:
189125068
result:
ok 1 number(s): "189125068"
Test #17:
score: 0
Accepted
time: 23ms
memory: 23124kb
input:
5 4
output:
975434093
result:
ok 1 number(s): "975434093"
Test #18:
score: 0
Accepted
time: 251ms
memory: 30980kb
input:
1000 1000
output:
720037464
result:
ok 1 number(s): "720037464"
Test #19:
score: 0
Accepted
time: 22ms
memory: 23056kb
input:
72 42
output:
638177567
result:
ok 1 number(s): "638177567"
Test #20:
score: 0
Accepted
time: 23ms
memory: 23080kb
input:
15 19
output:
663050288
result:
ok 1 number(s): "663050288"
Test #21:
score: 0
Accepted
time: 31ms
memory: 23180kb
input:
68 89
output:
94365047
result:
ok 1 number(s): "94365047"
Test #22:
score: 0
Accepted
time: 26ms
memory: 23160kb
input:
92 37
output:
652605307
result:
ok 1 number(s): "652605307"
Test #23:
score: 0
Accepted
time: 31ms
memory: 23160kb
input:
61 87
output:
498277867
result:
ok 1 number(s): "498277867"
Test #24:
score: 0
Accepted
time: 22ms
memory: 23044kb
input:
81 40
output:
133095344
result:
ok 1 number(s): "133095344"
Test #25:
score: 0
Accepted
time: 22ms
memory: 23080kb
input:
7 91
output:
524164813
result:
ok 1 number(s): "524164813"
Test #26:
score: 0
Accepted
time: 25ms
memory: 23028kb
input:
31 18
output:
361233485
result:
ok 1 number(s): "361233485"
Test #27:
score: 0
Accepted
time: 18ms
memory: 23124kb
input:
74 54
output:
500686087
result:
ok 1 number(s): "500686087"
Test #28:
score: 0
Accepted
time: 26ms
memory: 23080kb
input:
32 2
output:
586504335
result:
ok 1 number(s): "586504335"
Test #29:
score: 0
Accepted
time: 125ms
memory: 26864kb
input:
656 718
output:
346764298
result:
ok 1 number(s): "346764298"
Test #30:
score: 0
Accepted
time: 78ms
memory: 24768kb
input:
254 689
output:
358078813
result:
ok 1 number(s): "358078813"
Test #31:
score: 0
Accepted
time: 129ms
memory: 26924kb
input:
713 674
output:
914437613
result:
ok 1 number(s): "914437613"
Test #32:
score: 0
Accepted
time: 46ms
memory: 23684kb
input:
136 698
output:
56687290
result:
ok 1 number(s): "56687290"
Test #33:
score: 0
Accepted
time: 91ms
memory: 24684kb
input:
369 401
output:
312325811
result:
ok 1 number(s): "312325811"
Test #34:
score: 0
Accepted
time: 35ms
memory: 23180kb
input:
280 204
output:
280012063
result:
ok 1 number(s): "280012063"
Test #35:
score: 0
Accepted
time: 73ms
memory: 24780kb
input:
904 225
output:
162909174
result:
ok 1 number(s): "162909174"
Test #36:
score: 0
Accepted
time: 237ms
memory: 31016kb
input:
855 928
output:
39885159
result:
ok 1 number(s): "39885159"
Test #37:
score: 0
Accepted
time: 76ms
memory: 24716kb
input:
503 365
output:
745115888
result:
ok 1 number(s): "745115888"
Test #38:
score: 0
Accepted
time: 234ms
memory: 31012kb
input:
646 996
output:
610925577
result:
ok 1 number(s): "610925577"
Test #39:
score: 0
Accepted
time: 241ms
memory: 31016kb
input:
990 918
output:
203469632
result:
ok 1 number(s): "203469632"
Test #40:
score: 0
Accepted
time: 255ms
memory: 30984kb
input:
961 949
output:
169566857
result:
ok 1 number(s): "169566857"
Test #41:
score: 0
Accepted
time: 247ms
memory: 31024kb
input:
946 932
output:
352423195
result:
ok 1 number(s): "352423195"
Test #42:
score: 0
Accepted
time: 255ms
memory: 30876kb
input:
903 981
output:
196309824
result:
ok 1 number(s): "196309824"
Test #43:
score: 0
Accepted
time: 239ms
memory: 30952kb
input:
916 988
output:
487208972
result:
ok 1 number(s): "487208972"
Test #44:
score: 0
Accepted
time: 238ms
memory: 30916kb
input:
982 982
output:
387421488
result:
ok 1 number(s): "387421488"
Test #45:
score: 0
Accepted
time: 243ms
memory: 30964kb
input:
955 911
output:
955637031
result:
ok 1 number(s): "955637031"
Test #46:
score: 0
Accepted
time: 238ms
memory: 30900kb
input:
906 999
output:
798469943
result:
ok 1 number(s): "798469943"
Test #47:
score: 0
Accepted
time: 239ms
memory: 30916kb
input:
982 975
output:
193506289
result:
ok 1 number(s): "193506289"
Test #48:
score: 0
Accepted
time: 248ms
memory: 31032kb
input:
921 991
output:
431202149
result:
ok 1 number(s): "431202149"