summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--math/chineseRemainder.cpp47
-rw-r--r--math/transforms/seriesOperations.cpp55
2 files changed, 68 insertions, 34 deletions
diff --git a/math/chineseRemainder.cpp b/math/chineseRemainder.cpp
index 623f94b..a1aa480 100644
--- a/math/chineseRemainder.cpp
+++ b/math/chineseRemainder.cpp
@@ -1,35 +1,14 @@
-// Laufzeit: O(n * log(n)), n := Anzahl der Kongruenzen. Nur für
-// teilerfremde Moduli. Berechnet das kleinste, nicht negative x,
-// das alle Kongruenzen simultan löst. Alle Lösungen sind
-// kongruent zum kgV der Moduli (Produkt, da teilerfremd).
-struct ChineseRemainder {
- using lll = __int128;
- vector<lll> lhs, rhs, mods, inv;
- lll M; // Produkt über die Moduli. Kann leicht überlaufen.
+struct CRT{
+ using lll = __int128_t;
+ lll M = 1, sol = 0; // Solution unique modulo M
+ bool hasSol = true;
- ll g(const vector<lll> &vec) {
- lll res = 0;
- for (int i = 0; i < sz(vec); i++) {
- res += (vec[i] * inv[i]) % M;
- res %= M;
- }
- return res;
- }
-
- // Fügt Kongruenz l * x = r (mod m) hinzu.
- void addEquation(ll l, ll r, ll m) {
- lhs.push_back(l);
- rhs.push_back(r);
- mods.push_back(m);
- }
-
- ll solve() { // Löst das System.
- M = accumulate(all(mods), lll(1), multiplies<lll>());
- inv.resize(sz(lhs));
- for (int i = 0; i < sz(lhs); i++) {
- lll x = (M / mods[i]) % mods[i];
- inv[i] = (multInv(x, mods[i]) * (M / mods[i]));
- }
- return (multInv(g(lhs), M) * g(rhs)) % M;
- }
-};
+ // Adds congruence x = a (mod m)
+ void add(ll a, ll m){
+ ll s, t, d = extendedEuclid(M, m, s, t);
+ if((a - sol) % d != 0) hasSol = false;
+ lll z = M/d * s;
+ M *= m/d;
+ sol = (z % M * (a-sol) % M + sol + M) % M;
+ }
+}; \ No newline at end of file
diff --git a/math/transforms/seriesOperations.cpp b/math/transforms/seriesOperations.cpp
new file mode 100644
index 0000000..3851a1e
--- /dev/null
+++ b/math/transforms/seriesOperations.cpp
@@ -0,0 +1,55 @@
+vector<ll> poly_inv(vector<ll> a, int n){
+ vector<ll> q = {powMod(a[0], mod-2, mod)};
+ for(int len = 1; len < n; len *= 2){
+ vector<ll> a2 = a, q2 = q;
+ a2.resize(2*len), q2.resize(2*len);
+ ntt(q2);
+ for(int j = 0; j < 2; j++){
+ ntt(a2);
+ for(int i = 0; i < 2*len; i++) a2[i] = a2[i] * q2[i] % mod;
+ ntt(a2, true);
+ for(int i = 0; i < len; i++) a2[i] = 0;
+ }
+ for(int i = len; i < min(n, 2*len); i++) q.push_back((mod - a2[i]) % mod);
+ }
+ return q;
+}
+
+vector<ll> poly_deriv(vector<ll> a){
+ for(int i = 0; i < sz(a)-1; i++)
+ a[i] = a[i+1] * (i+1) % mod;
+ a.pop_back();
+ return a;
+}
+
+vector<ll> poly_integr(vector<ll> a){
+ if(a.empty()) return {0};
+ a.push_back(a.back() * powMod(sz(a), mod-2, mod) % mod);
+ for(int i = sz(a)-2; i > 0; i--)
+ a[i] = a[i-1] * powMod(i, mod-2, mod) % mod;
+ a[0] = 0;
+ return a;
+}
+
+vector<ll> poly_log(vector<ll> a, int n){
+ a = mul(poly_deriv(a), poly_inv(a, n));
+ a.resize(n-1);
+ a = poly_integr(a);
+ return a;
+}
+
+vector<ll> poly_exp(vector<ll> a, int n){
+ vector<ll> q = {1};
+ for(int len = 1; len < n; len *= 2){
+ vector<ll> p = poly_log(q, 2*len);
+ for(int i = 0; i < 2*len; i++)
+ p[i] = (mod - p[i] + (i < sz(a) ? a[i] : 0)) % mod;
+ vector<ll> q2 = q;
+ q2.resize(2*len);
+ ntt(p), ntt(q2);
+ for(int i = 0; i < 2*len; i++) p[i] = p[i] * q2[i] % mod;
+ ntt(p, true);
+ for(int i = len; i < min(n, 2*len); i++) q.push_back(p[i]);
+ }
+ return q;
+} \ No newline at end of file