QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#482868 | #9120. Huge Segment Tree | ucup-team2307 | TL | 687ms | 119812kb | C++20 | 3.8kb | 2024-07-17 23:09:40 | 2024-07-17 23:09:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pii = pair<int, int>;
using vi = vector<int>;
#define fi first
#define se second
#define pb push_back
const ll mod = (119 << 23) + 1, root = 62; // = 998244353
int bpow(int a, int b)
{
int r = 1;
while (b>0)
{
if (b&1)
r = r*a%mod;
a = a*a%mod;
b /= 2;
}
return r;
}
#define modpow bpow
// For p < 2^30 there is also e.g. 5 << 25, 7 << 26, 479 << 21
// and 483 << 21 (same root). The last two are > 10^9.
typedef vector<ll> vl;
void ntt(vl &a) {
int n = sz(a), L = 31 - __builtin_clz(n);
static vl rt(2, 1);
for (static int k = 2, s = 2; k < n; k *= 2, s++) {
rt.resize(n);
ll z[] = {1, modpow(root, mod >> s)};
rep(i,k,2*k) rt[i] = rt[i / 2] * z[i & 1] % mod;
}
vi rev(n);
rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int k = 1; k < n; k *= 2)
for (int i = 0; i < n; i += 2 * k) rep(j,0,k) {
ll z = rt[j + k] * a[i + j + k] % mod, &ai = a[i + j];
a[i + j + k] = ai - z + (z > ai ? mod : 0);
ai += (ai + z >= mod ? z - mod : z);
}
}
vl conv(const vl &a, const vl &b) {
if (a.empty() || b.empty()) return {};
int s = sz(a) + sz(b) - 1, B = 32 - __builtin_clz(s), n = 1 << B;
int inv = modpow(n, mod - 2);
vl L(a), R(b), out(n);
L.resize(n), R.resize(n);
ntt(L), ntt(R);
rep(i,0,n) out[-i & (n - 1)] = (ll)L[i] * R[i] % mod * inv % mod;
ntt(out);
return {out.begin(), out.begin() + s};
}
//const int mod = 998244353;
const int RL = 19;
const int R = 1<<RL;
const int N = R+100;
int n;
vl a[RL+1], b[R+1];
int lg[N];
vl add(vl a, vl b)
{
vl c(max(a.size(), b.size()));
for (int i=0; i<int(a.size()); i++)
c[i] += a[i];
for (int i=0; i<int(b.size()); i++)
c[i] += b[i];
for (int& i : c)
i %= mod;
return c;
}
vl solve(int l, int r)
{
// cout<<l<<" "<<r<<endl;
if (r-l+1 <= 2)
{
vl rb = {};
for (int i=l; i<=r; i++)
if (b[i].size() > 0)
{
if (rb.size() > 0)
{
rb.pb(0);
rb.pb(0);
for (int i=int(rb.size())-1; i>=2; i--)
rb[i] = rb[i] + 2*rb[i-1] + rb[i-2];
rb[1] += 2*rb[0];
rb[0] += b[i][0];
rb[1] += b[i][1];
for (int& i : rb)
i %= mod;
}
else
rb = b[i];
}
return rb;
}
else
{
int m = (l+r)/2;
int mi = lg[r-l+1];
auto A = solve(l, m);
auto B = solve(m+1, r);
//(A1*x+A2)*B1+B2
return add(conv(A, a[mi-1]), B);
}
}
int fact[2*N];
int rfact[2*N];
int cnk(int n, int k)
{
return fact[n]*rfact[n-k]%mod*rfact[k]%mod;
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
lg[1] = 0;
for (int i=2; i<N; i++)
lg[i] = lg[i/2]+1;
fact[0] = rfact[0] = 1;
for (int i=1; i<N; i++)
fact[i] = fact[i-1]*i%mod, rfact[i] = bpow(fact[i], mod-2);
cin>>n;
for (int i=1; i<=n; i++)
{
// a[i] = {1, 2, 1};
b[R-n+i] = {(bpow(2, i)+mod-2)%mod, (bpow(2, i-1)+mod-1)%mod };
}
a[0] = {1, 2, 1};
for (int i=1; i<=RL; i++)
a[i] = conv(a[i-1], a[i-1]);
auto ans = solve(1, R);
ans.resize(2*n-2);
for (int i=0; i<n; i++)
ans[i] = (ans[i] + 2*cnk(n, i+1))%mod;
ans[0]++;
for (int i : ans)
cout<<i<<" ";
cout<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 669ms
memory: 118916kb
input:
2
output:
7 3
result:
ok 2 tokens
Test #2:
score: 0
Accepted
time: 687ms
memory: 119700kb
input:
3
output:
15 14 6 1
result:
ok 4 tokens
Test #3:
score: 0
Accepted
time: 669ms
memory: 118548kb
input:
4
output:
31 43 36 19 6 1
result:
ok 6 tokens
Test #4:
score: 0
Accepted
time: 672ms
memory: 118140kb
input:
5
output:
63 110 132 114 70 30 8 1
result:
ok 8 tokens
Test #5:
score: 0
Accepted
time: 687ms
memory: 119812kb
input:
6
output:
127 255 384 448 400 272 136 47 10 1
result:
ok 10 tokens
Test #6:
score: 0
Accepted
time: 678ms
memory: 119512kb
input:
7
output:
255 558 978 1401 1610 1478 1066 589 240 68 12 1
result:
ok 12 tokens
Test #7:
score: 0
Accepted
time: 673ms
memory: 119812kb
input:
8
output:
511 1179 2292 3803 5250 5987 5576 4183 2482 1137 388 93 14 1
result:
ok 14 tokens
Test #8:
score: 0
Accepted
time: 678ms
memory: 118072kb
input:
9
output:
1023 2438 5088 9398 14896 20038 22632 21250 16406 10282 5144 2006 588 122 16 1
result:
ok 16 tokens
Test #9:
score: 0
Accepted
time: 678ms
memory: 118940kb
input:
10
output:
2047 4975 10896 21772 38360 58724 77184 86312 81448 64324 42112 22576 9744 3304 848 155 18 1
result:
ok 18 tokens
Test #10:
score: 0
Accepted
time: 661ms
memory: 118952kb
input:
11
output:
4095 10070 22782 48209 92140 156292 232068 298744 330926 313422 252186 171122 97008 45368 17200 5155 1176 192 20 1
result:
ok 20 tokens
Test #11:
score: -100
Time Limit Exceeded
input:
500000