GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — code-library/Math/NTT With Any Prime MOD.cpp at master · SuckSolace/code-library Expand file tree/ Copy pathNTT With Any Prime MOD.cppMore file actions 126 lines (123 loc) · 3.83 KB / Copy pathNTT With Any Prime MOD.cppFile metadata and controls126 lines (123 loc) · 3.83 KB 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 #include<bits/stdc++.h> using namespace std; const int N = 3e5 + 9, mod = 998244353; struct base { double x, y; base() { x = y = 0; } base(double x, double y): x(x), y(y) { } }; inline base operator + (base a, base b) { return base(a.x + b.x, a.y + b.y); } inline base operator - (base a, base b) { return base(a.x - b.x, a.y - b.y); } inline base operator * (base a, base b) { return base(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } inline base conj(base a) { return base(a.x, -a.y); } int lim = 1; vector<base> roots = {{0, 0}, {1, 0}}; vector<int> rev = {0, 1}; const double PI = acosl(- 1.0); void ensure_base(int p) { if(p <= lim) return; rev.resize(1 << p); for(int i = 0; i < (1 << p); i++) rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (p - 1)); roots.resize(1 << p); while(lim < p) { double angle = 2 * PI / (1 << (lim + 1)); for(int i = 1 << (lim - 1); i < (1 << lim); i++) { roots[i << 1] = roots[i]; double angle_i = angle * (2 * i + 1 - (1 << lim)); roots[(i << 1) + 1] = base(cos(angle_i), sin(angle_i)); } lim++; } } void fft(vector<base> &a, int n = -1) { if(n == -1) n = a.size(); assert((n & (n - 1)) == 0); int zeros = __builtin_ctz(n); ensure_base(zeros); int shift = lim - zeros; for(int i = 0; i < n; i++) if(i < (rev[i] >> shift)) swap(a[i], a[rev[i] >> shift]); for(int k = 1; k < n; k <<= 1) { for(int i = 0; i < n; i += 2 * k) { for(int j = 0; j < k; j++) { base z = a[i + j + k] * roots[j + k]; a[i + j + k] = a[i + j] - z; a[i + j] = a[i + j] + z; } } } } //eq = 0: 4 FFTs in total //eq = 1: 3 FFTs in total vector<int> multiply(vector<int> &a, vector<int> &b, int eq = 0) { int need = a.size() + b.size() - 1; int p = 0; while((1 << p) < need) p++; ensure_base(p); int sz = 1 << p; vector<base> A, B; if(sz > (int)A.size()) A.resize(sz); for(int i = 0; i < (int)a.size(); i++) { int x = (a[i] % mod + mod) % mod; A[i] = base(x & ((1 << 15) - 1), x >> 15); } fill(A.begin() + a.size(), A.begin() + sz, base{0, 0}); fft(A, sz); if(sz > (int)B.size()) B.resize(sz); if(eq) copy(A.begin(), A.begin() + sz, B.begin()); else { for(int i = 0; i < (int)b.size(); i++) { int x = (b[i] % mod + mod) % mod; B[i] = base(x & ((1 << 15) - 1), x >> 15); } fill(B.begin() + b.size(), B.begin() + sz, base{0, 0}); fft(B, sz); } double ratio = 0.25 / sz; base r2(0, - 1), r3(ratio, 0), r4(0, - ratio), r5(0, 1); for(int i = 0; i <= (sz >> 1); i++) { int j = (sz - i) & (sz - 1); base a1 = (A[i] + conj(A[j])), a2 = (A[i] - conj(A[j])) * r2; base b1 = (B[i] + conj(B[j])) * r3, b2 = (B[i] - conj(B[j])) * r4; if(i != j) { base c1 = (A[j] + conj(A[i])), c2 = (A[j] - conj(A[i])) * r2; base d1 = (B[j] + conj(B[i])) * r3, d2 = (B[j] - conj(B[i])) * r4; A[i] = c1 * d1 + c2 * d2 * r5; B[i] = c1 * d2 + c2 * d1; } A[j] = a1 * b1 + a2 * b2 * r5; B[j] = a1 * b2 + a2 * b1; } fft(A, sz); fft(B, sz); vector<int> res(need); for(int i = 0; i < need; i++) { long long aa = A[i].x + 0.5; long long bb = B[i].x + 0.5; long long cc = A[i].y + 0.5; res[i] = (aa + ((bb % mod) << 15) + ((cc % mod) << 30))%mod; } return res; } vector<int> pow(vector<int>& a, int p) { vector<int> res; res.emplace_back(1); while(p) { if(p & 1) res = multiply(res, a); a = multiply(a, a, 1); p >>= 1; } return res; } int main() { int n, k; cin >> n >> k; vector<int> a(10, 0); while(k--) { int m; cin >> m; a[m] = 1; } vector<int> ans = pow(a, n / 2); int res = 0; for(auto x: ans) res = (res + 1LL * x * x % mod) % mod; cout << res << '\n'; return 0; } //https://codeforces.com/contest/1096/problem/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. |