QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#210114 | #6323. Range NEQ | ucup-team870 | AC ✓ | 666ms | 46348kb | C++17 | 8.4kb | 2023-10-11 01:47:56 | 2023-10-11 01:47:56 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define per(i,r,l) for(int i=r; i>=l; i--)
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define vi vector<int>
#define poly vi
const int N=1e6+6,mod=998244353,G=3;
ll qp(ll x,ll y){
ll res=1;
while(y){
if(y&1)res=res*x%mod;
x=x*x%mod; y>>=1;
} return res;
}
const int Gi=qp(G,mod-2);
ll fac[N],inv[N];
ll C(int i,int j){
return fac[i]*inv[j]%mod*inv[i-j]%mod;
}
ll A(int i,int j){
return fac[i]*inv[i-j]%mod;
}
namespace Poly {
int limit = 1, L = 0; int r[N * 4];
void NTT(poly& A, int type) { //下标在[0,limit)范围内,数组开四倍即可
A.resize(limit);
for (int i = 0; i < limit; i++)
if (i < r[i]) swap(A[i], A[r[i]]);
for (int mid = 1; mid < limit; mid <<= 1) {
ll Wn = qp(type == 1 ? G : Gi, (mod - 1) / (mid << 1)); //G是模数的原根,Gi是逆元
for (int j = 0; j < limit; j += (mid << 1)) {
ll w = 1; //ll不一定够
for (int k = 0; k < mid; k++, w = (w * Wn) % mod) {
int x = A[j + k], y = w * A[j + k + mid] % mod; //int不一定够
A[j + k] = (x + y) % mod, A[j + k + mid] = (x - y + mod) % mod;
}
}
}
}
poly operator + (poly a, poly b) {
int n = max(a.size(), b.size());
a.resize(n); b.resize(n);
rep(i, 0, n - 1)a[i] = (a[i] + b[i]) % mod;
return a;
}
poly operator - (poly a, poly b) {
int n = max(a.size(), b.size());
a.resize(n); b.resize(n);
rep(i, 0, n - 1)a[i] = (a[i] - b[i] + mod) % mod;
return a;
}
void poly_mul_init(poly& a, poly& b) {
limit = 1; L = 0;
int N = a.size() - 1, M = b.size() - 1;
while (limit <= N + M) limit <<= 1, L++;
for (int i = 0; i < limit; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (L - 1));
}
poly poly_mul(poly a, poly b) { //原先的a,b不需要维持原状的话,可以加&
int n = a.size() + b.size() - 1;
poly_mul_init(a, b);
NTT(a, 1); NTT(b, 1);
for (int i = 0; i < limit; i++) a[i] = 1ll * a[i] * b[i] % mod; //a[i]为ll不一定够
NTT(a, -1);
ll INV = qp(limit, mod - 2);
rep(i, 0, limit - 1)a[i] = a[i] * INV % mod;
a.resize(n); return a;
}
poly poly_inv(poly& a, int n) { //n为答案阶数, %(x^(n+1))意义下
if (a.size() < n + 1)a.resize(n + 1); //!!!!
if (n == 0)return { (int)qp(a[0],mod - 2) };
int n2 = n / 2;
poly b = poly_inv(a, n2);
poly ta(n + 1); rep(i, 0, n)ta[i] = a[i];
/*等价于:
poly ans=poly_mul(ta,b); ans.resize(n+1);
rep(i,0,n)ans[i]=(((i==0)?2:0)-ans[i]+mod)%mod;
ans=poly_mul(b,ans); ans.resize(n+1); return ans;*/
b.resize(n + 1); //ta和b的阶数一定要相同
poly_mul_init(ta, b);
NTT(ta, 1); NTT(b, 1);
rep(i, 0, limit)b[i] = (2 - 1ll * ta[i] * b[i] % mod + mod) * b[i] % mod; //蝴蝶变换这里每一项2-,然而多项式乘法时还是{2,0,0...}
NTT(b, -1);
ll inv = qp(limit, mod - 2);
rep(i, 0, n)b[i] = b[i] * inv % mod;
b.resize(n + 1); ta.clear(); ta.shrink_to_fit();
return b;
}
poly poly_derivate(poly& a) { //求导
int n = (int)a.size() - 1; poly da(n + 1);
rep(i, 1, n)da[i - 1] = 1ll * a[i] * i % mod;
return da;
}
poly poly_integral(poly& a) { //积分
int n = (int)a.size(); poly ia(n + 1);
rep(i, 1, n)ia[i] = 1ll * a[i - 1] * qp(i, mod - 2) % mod;
return ia;
}
poly poly_ln(poly a) {
int n = (int)a.size() - 1;
assert(a[0] == 1);
poly da = poly_derivate(a);
a = poly_inv(a, n);
poly b = poly_mul(a, da); b.resize(n + 1);
vi res = poly_integral(b); res.resize(n + 1); return res;
}
poly poly_exp(poly& a, int n) { //牛顿迭代, nxtg = g*(1-ln(g)+A)
if (n == 0) {
assert(a[0] == 0); return { 1 };
}
int n2 = n / 2;
poly g = poly_exp(a, n2); g.resize(n + 1);//这里求逆要扩展g
poly lng = poly_ln(g);
rep(i, 0, n)lng[i] = ((i == 0) + a[i] - lng[i] + mod) % mod;
poly res = poly_mul(g, lng); res.resize(n + 1);
return res;
}
//多项式ln+exp可以用来优化多项式快速幂:f(x)^m = exp(ln(f(x))*m)
poly poly_pow(poly a, int y) { //a[0]=1. a[0]=0的时候直接移位; 非0的时候整个多项式/a[0],最终结果再*qp(a[0],y)
//如果len(a)*y很小的话,直接把点值做y次方即可
assert(a[0] == 1);
int n = (int)a.size() - 1;
poly lna = poly_ln(a);
rep(i, 0, n)lna[i] = 1ll * lna[i] * y % mod;
return poly_exp(lna, n);
}
void cdq_fft(poly& f, poly& g, int l, int r) { //f=F(f)*g形式,考虑[l,mid]对[mid+1,r]的贡献,一次poly_mul即可
if (l == r)return;
int mid = l + r >> 1;
cdq_fft(f, g, l, mid);
poly b(r - l + 1); rep(i, 0, r - l)b[i] = g[i];
poly a(mid - l + 1); rep(i, 0, mid - l)a[i] = f[i + l];
poly res = poly_mul(a, b);
rep(i, mid + 1, r)f[i] = (f[i] + res[i - l]) % mod;
cdq_fft(f, g, mid + 1, r);
}
pair<poly, poly>poly_divison(poly f, poly g) { //f=q*g+r, f(n),g(m),q(n-m),r(m-1)
int m = (int)g.size() - 1;
if (f.size() < m + 1)f.resize(m + 1); int n = (int)f.size() - 1;
vi fR = f; reverse(fR.begin(), fR.end()); fR.resize(n - m + 1);//快了很多!!
vi gR = g; reverse(gR.begin(), gR.end());
auto qR = poly_mul(poly_inv(gR, n - m), fR); qR.resize(n - m + 1);
auto q = qR; reverse(q.begin(), q.end());
auto r = f - poly_mul(q, g); r.resize(m);
return { q,r };
}
int linear_recurrence_poly(vi a, vi f, int n) { //常系数齐次线性递推,求a_n.
//a_i=sigma(a_{i-j}*f_j),1<=j<=k. 已知a的[0,k-1]项
int k = a.size(); assert(f.size() == k + 1);
if (n < k)return a[n];
vi p(k + 1); p[k] = 1; rep(i, 1, k)p[k - i] = (mod - f[i]) % mod;
vi res = { 1 }, x = { 0,1 };
vi gR = p; reverse(gR.begin(), gR.end()); vi invgR = poly_inv(gR, k); //gR的逆预处理出来
auto poly_division_r = [&](poly f, poly g)->poly {
int m = (int)g.size() - 1;
if (f.size() < m + 1)f.resize(m + 1); int n = (int)f.size() - 1;
vi fR = f; reverse(fR.begin(), fR.end()); fR.resize(n - m + 1);
auto qR = poly_mul(invgR, fR); qR.resize(n - m + 1);
auto q = qR; reverse(q.begin(), q.end());
auto r = f - poly_mul(q, g); r.resize(m);
return r;
};
while (n) {
int nn = x.size() * 2 - 1;
poly_mul_init(x, x);
NTT(x, 1);
if (n & 1) {
NTT(res, 1);
for (int i = 0; i < limit; i++) res[i] = 1ll * res[i] * x[i] % mod;
}
for (int i = 0; i < limit; i++)x[i] = 1ll * x[i] * x[i] % mod;
NTT(x, -1);
ll INV = qp(limit, mod - 2);
if (n & 1) {
NTT(res, -1);
for (int i = 0; i < limit; i++)res[i] = res[i] * INV % mod;
res.resize(nn);
}
for (int i = 0; i < limit; i++)x[i] = x[i] * INV % mod;
x.resize(nn);
if (n & 1)res = poly_division_r(res, p); //四次NTT即可
x = poly_division_r(x, p);
n >>= 1;
}
ll ans = 0;
rep(i, 0, k - 1)ans = (ans + 1ll * res[i] * a[i]) % mod; return ans;
}
};
int main() {
// IOS
const int M=1e6;
fac[0]=1; rep(i,1,M)fac[i]=fac[i-1]*i%mod;
inv[M]=qp(fac[M],mod-2); per(i,M,1)inv[i-1]=inv[i]*i%mod;
int n,m;cin>>n>>m;
vi a(m+1);
rep(i,0,m)a[i]=C(m,i)*A(m,i)%mod;
// rep(i,0,m)cout<<a[i]<<' '; cout<<'\n';
vi res={1}; int y=n;
while(y){
if(y&1)res=Poly::poly_mul(res,a);
a=Poly::poly_mul(a,a); y>>=1;
}
a=res;
ll ans=0,fl=1;
rep(i,0,n*m){
ans+=fl*fac[n*m-i]*a[i]%mod;
fl=-fl;
}
cout<<(ans%mod+mod)%mod<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 10ms
memory: 20328kb
input:
2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 7ms
memory: 20672kb
input:
5 1
output:
44
result:
ok 1 number(s): "44"
Test #3:
score: 0
Accepted
time: 16ms
memory: 19904kb
input:
167 91
output:
284830080
result:
ok 1 number(s): "284830080"
Test #4:
score: 0
Accepted
time: 6ms
memory: 19752kb
input:
2 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 10ms
memory: 20752kb
input:
2 3
output:
36
result:
ok 1 number(s): "36"
Test #6:
score: 0
Accepted
time: 9ms
memory: 20644kb
input:
2 4
output:
576
result:
ok 1 number(s): "576"
Test #7:
score: 0
Accepted
time: 9ms
memory: 20976kb
input:
3 1
output:
2
result:
ok 1 number(s): "2"
Test #8:
score: 0
Accepted
time: 10ms
memory: 20176kb
input:
3 2
output:
80
result:
ok 1 number(s): "80"
Test #9:
score: 0
Accepted
time: 10ms
memory: 20020kb
input:
3 3
output:
12096
result:
ok 1 number(s): "12096"
Test #10:
score: 0
Accepted
time: 11ms
memory: 20488kb
input:
3 4
output:
4783104
result:
ok 1 number(s): "4783104"
Test #11:
score: 0
Accepted
time: 6ms
memory: 19440kb
input:
4 1
output:
9
result:
ok 1 number(s): "9"
Test #12:
score: 0
Accepted
time: 10ms
memory: 20584kb
input:
4 2
output:
4752
result:
ok 1 number(s): "4752"
Test #13:
score: 0
Accepted
time: 3ms
memory: 21464kb
input:
4 3
output:
17927568
result:
ok 1 number(s): "17927568"
Test #14:
score: 0
Accepted
time: 10ms
memory: 20932kb
input:
4 4
output:
776703752
result:
ok 1 number(s): "776703752"
Test #15:
score: 0
Accepted
time: 3ms
memory: 19736kb
input:
5 2
output:
440192
result:
ok 1 number(s): "440192"
Test #16:
score: 0
Accepted
time: 10ms
memory: 20088kb
input:
5 3
output:
189125068
result:
ok 1 number(s): "189125068"
Test #17:
score: 0
Accepted
time: 11ms
memory: 21016kb
input:
5 4
output:
975434093
result:
ok 1 number(s): "975434093"
Test #18:
score: 0
Accepted
time: 666ms
memory: 46348kb
input:
1000 1000
output:
720037464
result:
ok 1 number(s): "720037464"
Test #19:
score: 0
Accepted
time: 13ms
memory: 19440kb
input:
72 42
output:
638177567
result:
ok 1 number(s): "638177567"
Test #20:
score: 0
Accepted
time: 11ms
memory: 19728kb
input:
15 19
output:
663050288
result:
ok 1 number(s): "663050288"
Test #21:
score: 0
Accepted
time: 14ms
memory: 20820kb
input:
68 89
output:
94365047
result:
ok 1 number(s): "94365047"
Test #22:
score: 0
Accepted
time: 13ms
memory: 19964kb
input:
92 37
output:
652605307
result:
ok 1 number(s): "652605307"
Test #23:
score: 0
Accepted
time: 13ms
memory: 19928kb
input:
61 87
output:
498277867
result:
ok 1 number(s): "498277867"
Test #24:
score: 0
Accepted
time: 13ms
memory: 20368kb
input:
81 40
output:
133095344
result:
ok 1 number(s): "133095344"
Test #25:
score: 0
Accepted
time: 3ms
memory: 20792kb
input:
7 91
output:
524164813
result:
ok 1 number(s): "524164813"
Test #26:
score: 0
Accepted
time: 10ms
memory: 20756kb
input:
31 18
output:
361233485
result:
ok 1 number(s): "361233485"
Test #27:
score: 0
Accepted
time: 13ms
memory: 21292kb
input:
74 54
output:
500686087
result:
ok 1 number(s): "500686087"
Test #28:
score: 0
Accepted
time: 0ms
memory: 21224kb
input:
32 2
output:
586504335
result:
ok 1 number(s): "586504335"
Test #29:
score: 0
Accepted
time: 441ms
memory: 40644kb
input:
656 718
output:
346764298
result:
ok 1 number(s): "346764298"
Test #30:
score: 0
Accepted
time: 146ms
memory: 26304kb
input:
254 689
output:
358078813
result:
ok 1 number(s): "358078813"
Test #31:
score: 0
Accepted
time: 466ms
memory: 41780kb
input:
713 674
output:
914437613
result:
ok 1 number(s): "914437613"
Test #32:
score: 0
Accepted
time: 101ms
memory: 25464kb
input:
136 698
output:
56687290
result:
ok 1 number(s): "56687290"
Test #33:
score: 0
Accepted
time: 130ms
memory: 26504kb
input:
369 401
output:
312325811
result:
ok 1 number(s): "312325811"
Test #34:
score: 0
Accepted
time: 51ms
memory: 21340kb
input:
280 204
output:
280012063
result:
ok 1 number(s): "280012063"
Test #35:
score: 0
Accepted
time: 143ms
memory: 26632kb
input:
904 225
output:
162909174
result:
ok 1 number(s): "162909174"
Test #36:
score: 0
Accepted
time: 622ms
memory: 42156kb
input:
855 928
output:
39885159
result:
ok 1 number(s): "39885159"
Test #37:
score: 0
Accepted
time: 154ms
memory: 26568kb
input:
503 365
output:
745115888
result:
ok 1 number(s): "745115888"
Test #38:
score: 0
Accepted
time: 554ms
memory: 42640kb
input:
646 996
output:
610925577
result:
ok 1 number(s): "610925577"
Test #39:
score: 0
Accepted
time: 660ms
memory: 43648kb
input:
990 918
output:
203469632
result:
ok 1 number(s): "203469632"
Test #40:
score: 0
Accepted
time: 640ms
memory: 45356kb
input:
961 949
output:
169566857
result:
ok 1 number(s): "169566857"
Test #41:
score: 0
Accepted
time: 650ms
memory: 45184kb
input:
946 932
output:
352423195
result:
ok 1 number(s): "352423195"
Test #42:
score: 0
Accepted
time: 649ms
memory: 45596kb
input:
903 981
output:
196309824
result:
ok 1 number(s): "196309824"
Test #43:
score: 0
Accepted
time: 642ms
memory: 43520kb
input:
916 988
output:
487208972
result:
ok 1 number(s): "487208972"
Test #44:
score: 0
Accepted
time: 664ms
memory: 44016kb
input:
982 982
output:
387421488
result:
ok 1 number(s): "387421488"
Test #45:
score: 0
Accepted
time: 655ms
memory: 45516kb
input:
955 911
output:
955637031
result:
ok 1 number(s): "955637031"
Test #46:
score: 0
Accepted
time: 650ms
memory: 43720kb
input:
906 999
output:
798469943
result:
ok 1 number(s): "798469943"
Test #47:
score: 0
Accepted
time: 657ms
memory: 44204kb
input:
982 975
output:
193506289
result:
ok 1 number(s): "193506289"
Test #48:
score: 0
Accepted
time: 634ms
memory: 43604kb
input:
921 991
output:
431202149
result:
ok 1 number(s): "431202149"