GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — code-library/Math/NTT.cpp at master · SuckSolace/code-library

Expand file tree

/

NTT.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
105 lines (104 loc) · 3.61 KB
/

NTT.cpp

Copy path

File metadata and controls

105 lines (104 loc) · 3.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
using namespace std;
const int N = 1 << 18;
const int mod = 998244353;
const int root = 3;
int lim, rev[N], w[N], wn[N], inv_lim;
void reduce(int &x) { x = (x + mod) % mod; }
int POW(int x, int y, int ans = 1) {
for (; y; y >>= 1, x = (long long) x * x % mod) if (y & 1) ans = (long long) ans * x % mod;
return ans;
}
void precompute(int len) {
lim = wn[0] = 1; int s = -1;
while (lim < len) lim <<= 1, ++s;
for (int i = 0; i < lim; ++i) rev[i] = rev[i >> 1] >> 1 | (i & 1) << s;
const int g = POW(root, (mod - 1) / lim);
inv_lim = POW(lim, mod - 2);
for (int i = 1; i < lim; ++i) wn[i] = (long long) wn[i - 1] * g % mod;
}
void ntt(vector<int> &a, int typ) {
for (int i = 0; i < lim; ++i) if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int i = 1; i < lim; i <<= 1) {
for (int j = 0, t = lim / i / 2; j < i; ++j) w[j] = wn[j * t];
for (int j = 0; j < lim; j += i << 1) {
for (int k = 0; k < i; ++k) {
const int x = a[k + j], y = (long long) a[k + j + i] * w[k] % mod;
reduce(a[k + j] += y - mod), reduce(a[k + j + i] = x - y);
}
}
}
if (!typ) {
reverse(a.begin() + 1, a.begin() + lim);
for (int i = 0; i < lim; ++i) a[i] = (long long) a[i] * inv_lim % mod;
}
}
vector<int> multiply(vector<int> &f, vector<int> &g) {
int n=(int)f.size() + (int)g.size() - 1;
precompute(n);
vector<int> a = f, b = g;
a.resize(lim); b.resize(lim);
ntt(a, 1), ntt(b, 1);
for (int i = 0; i < lim; ++i) a[i] = (long long) a[i] * b[i] % mod;
ntt(a, 0);
//while((int)a.size() && a.back() == 0) a.pop_back();
return a;
}
int fact[N], ifact[N];
vector<int> shift(vector<int> &f, int c) { //f(x + c)
int n=(int)f.size();
precompute(n + n - 1);
vector<int> a = f; a.resize(lim);
for (int i = 0; i < n; ++i) a[i] = (long long) a[i] * fact[i] % mod;
reverse(a.begin(), a.begin()+n);
vector<int> b; b.resize(lim); b[0] = 1;
for (int i = 1; i < n; ++i) b[i] = (long long) b[i - 1] * c % mod;
for (int i = 0; i < n; ++i) b[i] = (long long) b[i] * ifact[i] % mod;
ntt(a, 1), ntt(b, 1);
for (int i = 0; i < lim; ++i) a[i] = (long long) a[i] * b[i] % mod;
ntt(a, 0), reverse(a.begin(), a.begin() + n);
vector<int> g; g.resize(n);
for (int i = 0; i < n; ++i) g[i] = (long long) a[i] * ifact[i] % mod;
return g;
}
vector<int> range_mul(int n) { //(x+1)*(x+2)*(x+3)...(x+n)
if (n == 0) return vector<int>({1});
if (n & 1) {
vector<int> f = range_mul(n - 1);
f.push_back(0);
for (int i = (int)f.size()-1; i; --i) f[i] = (f[i - 1] + (long long) n * f[i]) % mod;
f[0] = (long long) f[0] * n % mod;
return f;
}
else {
int n_ = n >> 1;
vector<int> f = range_mul(n_);
vector<int> tmp = shift(f, n_);
f.resize(n_ + 1);
tmp.resize(n_ + 1);
return multiply(f, tmp);
}
}
int f(int n, int k) {
if(n == 0 && k == 0) return 1;
if(n <= 0 || k <= 0) return 0;
vector<int> x = vector<int>({0, 1});
vector<int> y = range_mul(n - 1);
vector<int> ans = multiply(x, y);
if(k >= (int)ans.size()) return 0;
return ans[k];
}
int ncr(int n, int r) {
if(r < 0 || n < r) return 0;
return 1LL * fact[n] * ifact[r] % mod * ifact[n - r] % mod;
}
int main() {
fact[0] = 1;
for (int i = 1; i < N; ++i) fact[i] = (long long) fact[i - 1] * i % mod;
ifact[N - 1] = POW(fact[N - 1], mod - 2);
for (int i = N - 1; i; --i) ifact[i - 1] = (long long) ifact[i] * i % mod;
int n, a, b; cin >> n >> a >> b;
cout << 1LL * f(n - 1, a + b - 2) * ncr(a + b - 2, a - 1) % mod << '\n';
return 0;
}
//https://codeforces.com/problemset/problem/960/G
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.