QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#482876 | #9120. Huge Segment Tree | ucup-team2307 | TL | 59ms | 15712kb | C++20 | 3.9kb | 2024-07-17 23:21:44 | 2024-07-17 23:21:45 |
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 N = 500500;
int n;
vl ap[N];
vl a[N], b[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;
}
pair<vl, vl> solve(int l, int r, bool starting = false)
{
if (r - l < 18)
{
if (ap[r-l].empty())
{
auto ra = a[l];
for (int i=l+1; i<=r; i++)
{
ra.pb(0);
ra.pb(0);
for (int i=int(ra.size())-1; i>=2; i--)
ra[i] = ra[i] + 2*ra[i-1] + ra[i-2];
ra[1] += 2*ra[0];
for (int& i : ra)
i %= mod;
}
ap[r-l] = ra;
}
auto rb = b[l];
for (int i=l+1; i<=r; i++)
{
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;
}
return {ap[r-l], rb};
}
else
{
int m = (l+r)/2;
auto A = solve(l, m);
auto B = solve(m+1, r);
//(A1*x+A2)*B1+B2
if (starting)
return {{}, add(conv(A.se, B.fi), B.se)};
return {conv(A.fi, B.fi), add(conv(A.se, B.fi), B.se)};
}
}
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);
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[i] = {(bpow(2, i)+mod-2)%mod, (bpow(2, i-1)+mod-1)%mod };
}
auto ans = solve(1, n, true).se;
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: 55ms
memory: 13880kb
input:
2
output:
7 3
result:
ok 2 tokens
Test #2:
score: 0
Accepted
time: 55ms
memory: 13592kb
input:
3
output:
15 14 6 1
result:
ok 4 tokens
Test #3:
score: 0
Accepted
time: 52ms
memory: 15564kb
input:
4
output:
31 43 36 19 6 1
result:
ok 6 tokens
Test #4:
score: 0
Accepted
time: 55ms
memory: 15024kb
input:
5
output:
63 110 132 114 70 30 8 1
result:
ok 8 tokens
Test #5:
score: 0
Accepted
time: 52ms
memory: 15712kb
input:
6
output:
127 255 384 448 400 272 136 47 10 1
result:
ok 10 tokens
Test #6:
score: 0
Accepted
time: 59ms
memory: 14200kb
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: 59ms
memory: 14464kb
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: 51ms
memory: 15260kb
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: 51ms
memory: 13172kb
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: 51ms
memory: 15320kb
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
output:
390220183 534638705 182393715 303662724 176884209 76063846 314206329 970463075 138271132 869076105 902568877 121426660 599330372 720576343 535733058 609095360 499854676 427738345 789967637 850801793 767689169 103101879 573005863 597231280 725469375 299015007 178535851 966708332 305629 940093777 7830...