Wwwwww unscrupulous propaganda about blog
Article list - nuclear fusion reactor core - Luo Gubo off
Congruence solving equations (Extended Chinese remainder theorem)
P4777 [template] to expand the Chinese Remainder Theorem (EXCRT)
To solve congruence equation as follows:
\(\begin{cases}x \equiv a_1\pmod {b_1}\\x \equiv a_2\pmod {b_2}\\......\\x \equiv a_n\pmod {b_n}\\\end{cases}\)
Wherein, \ (a_i, B_i \) non-negative integer, \ (B_1, B_2, ..., B_n \) necessarily coprime
Solving:
Assumed to have been obtained before the \ (k-1 \) Solutions of equations \ (x_ {k-1}
\) disposed \ (M = I = {LCM_. 1. 1} ^ {K}-BI \) , i.e. \ (M \) is a front \ (k-1 \) a modulus \ (B \) the least common multipleThen:
For the first \ (k-1 \) equations are satisfied \ (x_ {k-1}
+ tM \ equiv a_i \ pmod {b_i} \ \ (t \ in Z) \) ie: before \ (K -1 \) equations, general solution \ (x_ {k-1} + tM \ \ (t \ in Z) \)Desire to obtain a first \ (K \) solution of the equations, and the obtained solution, but also meet the first \ (k-1 \) equations
It:
is necessary to first \ (K \) a solution of the equation, the former \ (k-1 \) while passing the solutions of the equations also satisfies the first \ (K \) equations conditions.Provided: a first \ (K \) Solutions of equations \ (x_k = x_ {k- 1} + tM \ \ (t \ in Z) \)
Substituting this solution into the first \ (K \) equations may be obtained:
\ (X_ {K}. 1-tM + \ equiv a_k \ PMOD b_k} {\)
namely: \ (tM \ - K-equiv a_k-X_ { 1} \ pmod {b_k} \
) where: \ (M, a_k, K-X_ {}. 1, b_k \) are known.Use \ (exgcd \) solved this congruence equation can be obtained \ (T \) values.
The \ (T \) values are substituted back \ (K-x_k = X_ {} + tM. 1 \ \ (T \ in the Z) \) , can be obtained \ (x_k \) values
For \ (K \) after the time of the operation, the solution of equations can be obtained.
This question pit:
data range:
If you just open \ (long \ long \) , then when I get blown up anyway
In order to avoid blasting variables, use a technique called rapid multiplication modulo odd \ ((ba) \) wonderful \ ((ka) \) algorithm.
Rapid multiplication modulo
(Also known as turtle speed multiplier)Its essence and power quickly to take more than similar, are binary split applications.
Suppose \ (K \) a \ (A \) multiplying the
fast by its decomposed into \ (a \ times 2a \ times 4a \ times .... \) form.
This can take the edge side take the remainder to prevent bursting variables, data overflow.But the shift multiplication time complexity,
abruptly pulled \ (O (logn) \) levelll mul(ll A,ll B,ll mod) //快速乘取余 模板 { ll ans=0; while(B>0) { if(B & 1) ans=(ans+A%mod)%mod; A=(A+A)%mod; B>>=1; } return ans; }
On the code:
#include<cstdio>
using namespace std;
typedef long long ll;
ll n;
ll a[100010],b[100010];
ll mul(ll A,ll B,ll mod) //快速乘取余 模板
{
ll ans=0;
while(B>0)
{
if(B & 1) ans=(ans+A%mod)%mod;
A=(A+A)%mod;
B>>=1;
}
return ans;
}
ll exgcd(ll A,ll B,ll &x,ll &y) //扩展欧几里得 模板
{
if(!B)
{
x=1,y=0;
return A;
}
ll d=exgcd(B,A%B,x,y);
ll tmp=x;
x=y , y=tmp-A/B*y;
return d;
}
ll lcm(ll A,ll B) //求最小公倍数
{
ll xxx,yyy;
ll g=exgcd(A,B,xxx,yyy);
return (A/g*B);
}
ll excrt() //重点:求解同余方程组
{
ll x,y;
ll M=b[1],ans=a[1]; //赋初值
//M为前k-1个数的最小公倍数,ans为前k-1个方程的通解
for(int i=2;i<=n;i++)
{
ll A=M,B=b[i];
ll C=(a[i]-ans%B+B)%B; //代表同余方程 ax≡c(mod b) 中a,b,c
ll g=exgcd(A,B,x,y);
//求得A,B的最大公约数,与同余方程ax≡gcd(a,b)(mod b)的解,
if(C%g) return -1; //无解的情况
x=mul(x,C/g,B); //求得x的值,x即t
ans+=x*M; //获得前k个方程的通解
M=lcm(M,B); //更改M的值
ans=(ans%M+M)%M;
}
return ans;
}
int main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)
scanf("%lld%lld",&b[i],&a[i]);
ll ans=excrt();
printf("%lld",ans);
}