同余

“虽然我们相差甚远,但至少初心是一致的。”

同余

定义

ab 同余,当且仅当 ab 除以 p 的余数相同,记作 abp 同余,写作 ab(modp)

同余的性质

  • ab(modp) ,则有 a+cb+c(modp)

  • ab(modp) ,则有 acbc(modp)

  • ab(modp) ,且 cd(modp) ,则有 a±cb±d(modp)

  • acbc(modp) ,且 (c,m)=1 ,那么 ab(modp)

  • ab(modp) ,且 cd(modp) ,则有 acbd(modp)

  • ab(modp) ,则有 acbc(modpc)

  • ab 都是整数,且 dm 都是正整数, da,b,m 中任一公因数,则如果 ab(modm) 成立时,则有 adbd(modpd)

  • ab 都是整数,且 dm 都是正整数,且 d|m,则如果 ab(modm) 成立时,则有 ab(modd)

  • ab 都是整数,且 dm 都是正整数,如果 ab(modm) 成立时,则有 (a,m)=(b,m) ,且若如果 d 能够整除 a,b,m 中的任意一个, d 也就能整除 a,b 中的另一个。

扩展欧几里得算法

(a,b)=d ,那么对于方程 ax+by=d 的解有一组特殊解为 x=x0,y=y0 ,那么该方程的通解为 x=x0+bd×t,y=y0ad×t ,而我们用这个通解来逆向推出 gcd ,因为我们有欧几里得定理

(a,b)=d ,则有 (b,amodb)=d

那么就会推出: d=a×y1+b×(x1y1×ab)

在这个算式中满足: x=y1,y=x1ab×y1

扩欧解同余方程

对于方程 ax1(modb) ,已知 ab ,求出最小的 x

LuoguP1082

妥妥的扩欧模板题:

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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<iomanip>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define gh() getchar()
#define underMax(x,y) ((x)>(y)?(x):(y))
#define underMin(x,y) ((x)<(y)?(x):(y))
typedef long long LL;
using namespace std;
template<class T>
inline void underRead(T &x)
{
x=0;
char ch=gh(),t=0;
while(ch<'0'||ch>'9') t|=ch=='-',ch=gh();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh();
if(t) x=-x;
}
LL a,b,x,y;
inline void underExgcd(LL a,LL b)
{
if(!b)
{
x=1,y=7; //一对特殊解
return ;
}
underExgcd(b,a%b);
LL tx=x;
x=y;
y=tx-a/b*y;
}
int main()
{
// freopen("exgcd.in","r",stdin);
// freopen("exgcd.out","w",stdout);
underRead(a),underRead(b);
underExgcd(a,b);
x=(x%b+b)%b;
printf("%lld",x);
return 0;
}
/*
3 10
*/

中国剩余定理

LuoguP1495曹冲养猪

有一个同余方程组满足:

{xa1(modm1)xa2(modm2)xa3(modm3)xa4(modm4)xa5(modm5)......xak(modmk)

,求出最小的 x

思路

M=m1m2m3mk ,令 Mi=MmitiMi 的逆元,有 Miti1(modm)i

x=i=1kaiMiti

则可构造特殊解 x0=x+k×M

最小正整数解即为 xmin=x0modM

P3868 AC Code

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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<iomanip>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define gh() getchar()
#define underMax(x,y) ((x)>(y)?(x):(y))
#define underMin(x,y) ((x)<(y)?(x):(y))
typedef long long LL;
using namespace std;
const int MAXN=1e5+1;
template<class T>
inline void underRead(T &x)
{
x=0;
char ch=gh(),t=0;
while(ch<'0'||ch>'9') t|=ch=='-',ch=gh();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh();
if(t) x=-x;
}
int N;
struct Node
{
LL type,mod;
}Num[MAXN];
LL M[MAXN],Mul=1,Mi[MAXN],X;
inline void underExgcd(LL a,LL b,LL &x,LL &y)
{
if(!b)
{
x=1,y=0;
return ;
}
underExgcd(b,a%b,x,y);
LL z=x;x=y;y=z-y*(a/b);
}
int main()
{
// freopen("china.in","r",stdin);
// freopen("china.out","w",stdout);
underRead(N);
for(int i=1;i<=N;++i)
{
underRead(Num[i].type),underRead(Num[i].mod);
Mul*=Num[i].type;
}
for(int t=1;t<=N;++t)
{
Mi[t]=Mul/Num[t].type;
LL x=0,y=0;
underExgcd(Mi[t],Num[t].type,x,y);
X+=Num[t].mod*Mi[t]*(x<0?x+Num[t].type:x);
}
printf("%lld",X%Mul);
return 0;
}
/*
3
3 1
5 1
7 2
*/

Gitalk 加载中 ...