QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#236294 | #7632. Balanced Arrays | ucup-team870 | WA | 192ms | 53276kb | C++20 | 3.3kb | 2023-11-03 20:04:07 | 2023-11-03 20:04:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
#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 P pair<int,int>
#define ll long long
#define vi vector<int>
const int N =5e5+5, 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);
namespace Poly {
typedef vi 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++;
rep(i, 0, limit - 1)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);
rep(i, 0, limit - 1)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;
}
}
signed main(){
IOS
const int M=2e6;
vector<ll>fac(M+1),inv(M+1);
fac[0]=1; rep(i,1,M)fac[i]=i*fac[i-1]%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 fc(m+1),fd(m+1);
auto p2=[&](ll x){
return x*x%mod;
};
rep(c,1,m){
if(n+1-2*c<0)break;
// fc[c]=inv[2*c+1]*inv[n+1-2*c]%mod*p2(inv[c-1])%mod;
fc[c]=inv[n+1-2*c]%mod*p2(inv[c-1])%mod;
}
rep(d,0,m)fd[d]=fac[n+1+2*d]*p2(inv[d])%mod;
auto res=Poly::poly_mul(fc,fd);
ll ans=1;
auto C=[&](int i,int j){
if(i<j)return 0ll;
return fac[i]*inv[j]%mod*inv[i-j]%mod;
};
rep(k,1,m){
ans=(ans + p2(fac[k-1])*inv[2*k]%mod*res[k])%mod;
// rep(c,1,k) {
// ans=(ans+ C(n+1-2*c+2*k,2*k)*p2(C(k-1,c-1))%mod*qp(1,mod-2) )%mod;
// // cout<<c<<' '<<k<<" "<<p2(C(k-1,c-1))%mod*qp(1,mod-2)%mod<<endl;
// }
}
cout<<ans<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 17ms
memory: 34308kb
input:
2 2
output:
9
result:
ok 1 number(s): "9"
Test #2:
score: -100
Wrong Answer
time: 192ms
memory: 53276kb
input:
500000 500000
output:
220995671
result:
wrong answer 1st numbers differ - expected: '984531374', found: '220995671'