MCPcopy Create free account
hub / github.com/SuprDewd/CompetitiveProgramming / gcrt

Function gcrt

code/mathematics/crt.cpp:7–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5 rep(i,0,cnt) egcd(ns[i], l = N/ns[i], r, s), x += as[i]*s*l;
6 return smod(x, N); }
7pair<ll,ll> gcrt(vector<ll> &as, vector<ll> &ns) {
8 map<ll,pair<ll,ll> > ms;
9 rep(at,0,size(as)) {
10 ll n = ns[at];
11 for (ll i = 2; i*i <= n; i = i == 2 ? 3 : i + 2) {
12 ll cur = 1;
13 while (n % i == 0) n /= i, cur *= i;
14 if (cur > 1 && cur > ms[i].first)
15 ms[i] = make_pair(cur, as[at] % cur); }
16 if (n > 1 && n > ms[n].first)
17 ms[n] = make_pair(n, as[at] % n); }
18 vector<ll> as2, ns2; ll n = 1;
19 iter(it,ms) {
20 as2.push_back(it->second.second);
21 ns2.push_back(it->second.first);
22 n *= it->second.first; }
23 ll x = crt(as2,ns2);
24 rep(i,0,size(as)) if (smod(x,ns[i]) != smod(as[i],ns[i]))
25 return ii(0,0);
26 return make_pair(x,n); }
27// vim: cc=60 ts=2 sts=2 sw=2:

Callers 1

testFunction · 0.85

Calls 2

crtFunction · 0.85
smodFunction · 0.85

Tested by 1

testFunction · 0.68