Skip to content

Commit 54d566d

Browse files
committed
Update
1 parent d277e42 commit 54d566d

File tree

4 files changed

+117
-25
lines changed

4 files changed

+117
-25
lines changed

code/.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"iomanip": "cpp",
44
"vector": "cpp",
55
"cmath": "cpp",
6-
"iostream": "cpp"
6+
"iostream": "cpp",
7+
"numeric": "cpp",
8+
"tuple": "cpp"
79
}
810
}

code/math/CRT.cpp

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
1-
/*
2-
OVERFLOW!!!
3-
p = p1*p2*...*pk, pi pairwise coprime
1+
#include <bits/stdc++.h>
42

5-
a = a1 mod p1
6-
a = a2 mod p2
7-
..
8-
a = ak mod pk
3+
#define fi first
4+
#define se second
95

10-
This function returns
11-
a = x1 + x2*p1 + x3*p1*p2 + ... + xk*p1*p2*...*pk-1
6+
using namespace std;
127

13-
O(k^2)
14-
*/
8+
typedef long long ll;
9+
typedef pair<ll,ll> pll;
1510

16-
ll CRT(vector<ll> &a, vector<ll> &p){
17-
vector<ll> x(sz(p));
18-
ll ans = 0, prod = 1;
19-
forn(i, sz(p)){
20-
x[i] = a[i];
21-
forn(j,i){
22-
x[i] = 1LL*(x[i] - x[j])*binpow(p[j], p[i] - 2, p[i])%p[i];
23-
x[i] = (x[i] + p[i])%p[i];
24-
}
25-
ans+= 1LL*x[i]*prod;
26-
prod *= p[i];
27-
}
28-
return ans;
11+
pll extendedEuclid(ll a, ll b){ // a * x + b * y = __gcd(a,b)
12+
ll x,y;
13+
if (b==0) return {1,0};
14+
auto p=extendedEuclid(b,a%b);
15+
x=p.se;
16+
y=p.fi-(a/b)*x;
17+
if(a*x+b*y==-__gcd(a,b)) x=-x, y=-y;
18+
return {x,y};
19+
}
20+
pair<pll,pll> diophantine(ll a,ll b, ll r) {
21+
//a*x+b*y=r where r is multiple of __gcd(a,b);
22+
ll d=__gcd(a,b);
23+
a/=d; b/=d; r/=d;
24+
auto p = extendedEuclid(a,b);
25+
p.fi*=r; p.se*=r;
26+
// assert(a*p.fi+b*p.se==r);
27+
return {p,{-b,a}}; // solutions: p+t*ans.se
28+
}
29+
ll inv(ll a, ll m) {
30+
assert(__gcd(a,m)==1);
31+
ll x = diophantine(a,m,1).fi.fi;
32+
return ((x%m)+m)%m;
33+
}
34+
#define MOD(a,m) (((a)%m+m)%m)
35+
pll sol(tuple<ll,ll,ll> c){ //requires inv, diophantine
36+
ll a=get<0>(c), x1=get<1>(c), m=get<2>(c), d=__gcd(a,m);
37+
if(d==1) return pll(MOD(x1*inv(a,m),m), m);
38+
else return x1%d ? pll({-1LL,-1LL}) : sol(make_tuple(a/d,x1/d,m/d));
39+
}
40+
pair<ll,ll> crt(vector< tuple<ll,ll,ll> > &cond) { // returns: (sol, lcm)
41+
ll x1=0,m1=1,x2,m2;
42+
for(auto &t: cond){
43+
tie(x2,m2)=sol(t);
44+
if((x1-x2)%__gcd(m1,m2))return {-1,-1};
45+
if(m1==m2)continue;
46+
ll k=diophantine(m2,-m1,x1-x2).fi.se,l=m1*(m2/__gcd(m1,m2));
47+
x1=MOD((__int128_t)m1*k+x1,l); m1=l;
48+
}
49+
return sol(make_tuple(1,x1,m1));
50+
} //cond[i]={ai,bi,mi} ai*xi=bi (mi); assumes lcm fits in ll
51+
52+
int main(){
53+
// cout << "NO" << endl;
54+
return 0;
2955
}

code/math/berlekamp_massey.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// taken from https://codeforces.com/blog/entry/61306
2+
struct ber_ma{
3+
vi BM(vi &x){
4+
vi ls,cur; int lf,ld;
5+
forn(i,sz(x)){
6+
ll t=0;
7+
forn(j,sz(cur)) t=(t+x[i-j-1]*(ll)cur[j])%mod;
8+
if((t-x[i])%mod==0) continue;
9+
if(!sz(cur)){
10+
cur.resize(i+1);
11+
lf=i; ld=(t-x[i])%mod;
12+
continue;
13+
}
14+
ll k=-(x[i]-t)*inv(ld,mod);
15+
vi c(i-lf-1); c.pb(k);
16+
forn(j,sz(ls)) c.pb(-ls[j]*k%mod);
17+
if(sz(c)<sz(cur)) c.resize(sz(cur));
18+
forn(j,sz(cur)) c[j]=(c[j]+cur[j])%mod;
19+
if(i-lf+sz(ls)>=sz(cur)) ls=cur,lf=i,ld=(t-x[i])%mod;
20+
cur=c;
21+
}
22+
forn(i,sz(cur)) cur[i]=(cur[i]%mod+mod)%mod;
23+
return cur;
24+
}
25+
26+
int m; //length of recurrence
27+
//a: first terms
28+
//h: relation
29+
vector<ll> a, h, t_, s, t;
30+
//calculate p*q mod f
31+
inline vector<ll> mull(vector<ll> p, vector<ll> q){
32+
forn(i,2*m) t_[i]=0;
33+
forn(i,m) if(p[i])
34+
forn(j,m)
35+
t_[i+j]=(t_[i+j]+p[i]*q[j])%mod;
36+
for(int i=2*m-1;i>=m;--i) if(t_[i])
37+
forn(j,m)
38+
t_[i-j-1]=(t_[i-j-1]+t_[i]*h[j])%mod;
39+
forn(i,m) p[i]=t_[i];
40+
return p;
41+
}
42+
inline ll calc(ll k){
43+
if(k < sz(a)) return a[k];
44+
forn(i,m) s[i]=t[i]=0;
45+
s[0]=1;
46+
if(m!=1) t[1]=1;
47+
else t[0]=h[0];
48+
49+
while(k){
50+
if(k&1LL) s = mull(s,t);
51+
t = mull(t,t); k/=2;
52+
}
53+
ll su=0;
54+
forn(i,m) su=(su+s[i]*a[i])%mod;
55+
return (su%mod+mod)%mod;
56+
}
57+
ber_ma(vi &x){
58+
vi v = BM(x); m=sz(v);
59+
h.resize(m), a.resize(m), s.resize(m);
60+
t.resize(m), t_.resize(2*m);
61+
forn(i,m) h[i]=v[i],a[i]=x[i];
62+
}
63+
};

contents.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ math/CRT.cpp Chinese Remainder Theorem
9595
math/simplex.cpp Simplex
9696
math/gaussjordan.cpp Gauss Jordan
9797
math/gaussjordan_modular.cpp Gauss Jordan Modular
98+
math/berlekamp_massey.cpp Berlekamp Massey
9899

99100
[Dynamic Programming]
100101
dynamic-programming/edit_distance.cpp Edit Distance

0 commit comments

Comments
 (0)