### 1).扩展欧几里德的起手题——NOIP2012.同余方程

void ex_gcd(LL a, LL b,LL &x, LL &y)
{
if (!b){
x = 1; y = 0;
return;
} else {
ex_gcd(b, a % b, x, y);
int t = x;
x = y; y = t - a / b * y;
}
}

### 2).欧几里德的应用——bzoj1385.Division expression

#include <cstdio>

using namespace std;

const int MAX_N = 10005;

int T, n, a[MAX_N];

void init()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
}

int gcd(int a, int b)
{
return (b == 0) ? a : gcd(b, a % b);
}

bool judge()
{
a[2] /= gcd(a[1], a[2]);
for (int i = 3; i <= n; i ++) a[2] /= gcd(a[i], a[2]);
if (a[2] == 1) return 1;
return 0;
}

void doit()
{
if (judge()) printf("YES\n");
else printf("NO\n");
}

int main()
{
scanf("%d", &T);
while (T --){
init();
doit();
}
return 0;
}

### 3).更多的扩展欧几里德

#### 1.bzoj1477 && poj1061.青蛙的约会

#include <cstdio>

using namespace std;

typedef long long LL;

long long N, M, x, y, L;

void init()
{
scanf("%lld%lld%lld%lld%lld", &x, &y, &M, &N, &L);
}

LL gcd(LL a, LL b)
{
if (!b) return a;
return gcd(b, a % b);
}

void ex_gcd(LL a, LL b, LL &X, LL &Y)
{
if (!b){ X = 1, Y = 0; return; }
else {
ex_gcd(b, a % b, X, Y);
LL t = X;
X = Y; Y = t - a / b * X;
}
}

void doit(void)
{
LL a = M - N, b = L, c = y - x;
LL g = gcd(a, b);
if (c % g) { printf("Impossible\n"); return; }

LL X, Y;
ex_gcd(a, b, X, Y);
X = X * c / g;
X = (X % b + b) % b;
if (!X) X = b;
printf("%d\n", X);
return;
}

int main(void)
{
init(); doit();
return 0;
}

#### 2.poj2115.C Looooops

for (int i = a; i <= b; i += c, i %= 1 << k)能执行几次。死循环则输出 "FOREVER"

#include <cstdio>

using namespace std;

typedef long long LL;

LL i, j, l, k;

LL gcd(LL a, LL b)
{
return (b == 0) ? a : gcd(b, a % b);
}

void ex_gcd(LL a, LL b,LL &x, LL &y)
{
if (!b){
x = 1; y = 0;
return;
} else {
ex_gcd(b, a % b, x, y);
int t = x;
x = y; y = t - a / b * y;
}
}

void doit()
{
LL a = l, c = j - i, b = 1;
for (int p = 1; p <= k; p ++) b *= 2;
if (a < 0){
a *= -1; b *= -1;
}

LL x, y, d = gcd(a, b);
if (c % d != 0) { printf("FOREVER\n"); return; }

a /= d; b /= d; c /= d;
ex_gcd(a, b, x, y);
x = (((x * c) % b) + b) % b;
printf("%lld\n", x);
}

int main()
{
while (scanf("%lld%lld%lld%lld", &i, &j, &l, &k) != EOF){
if (i == 0 && j == 0 && l == 0 && k == 0) break;
doit();
}
return 0;
}

#### 3.bzoj1407.Savage(NOI2002)

#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 120;

int n, c[MAX_N], p[MAX_N], l[MAX_N];
int x, y, d, cnt = 0;

void init()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++) {
scanf("%d%d%d", &c[i], &p[i], &l[i]),
c[i] --,
cnt = max(c[i], cnt);
}
}

void ex_gcd(int a, int b, int &d, int &x, int &y)
{
if (!b){
x = 1; y = 0; d = a;
return;
} else {
ex_gcd(b, a % b, d, x, y);
int t = x;
x = y; y = t - a / b * x;
}
}

bool check(int m)
{
for (int i = 1; i <= n; i ++)
for (int j = i + 1; j <= n; j ++){
int a = p[i] - p[j], b = m, C = c[j] - c[i];
if (a < 0){
a *= -1; C *= -1;
}
ex_gcd(a, b, d, x, y);
if (C % d) continue;
int t = b / d;
x = x * C / d;
while (x < 0) x = x + t;
while (x >= t) x = x - t;
if (x <= l[i] && x <= l[j]) return 0;
}
return 1;
}

void doit()
{
for (int i = cnt + 1; i <= 1000000; i ++)
if (check(i)){ printf("%d\n", i); break; }
return;
}

int main()
{
init();
doit();
return 0;
} 

#### Android项目实战:简单天气

2016年11月25日 09:21

#### 扩展欧几里得定理的证明和代码

2017-01-25 19:07:16

#### 扩展欧几里德问题

2015年12月07日 65KB 下载

#### HDU 1576 A/B（扩展欧几里德变形）

2014-10-05 09:30:24

#### 扩展欧几里德（顺带说一下取模）

2016-08-03 23:47:20

#### 【扩展欧几里得】练习题

2015-05-13 20:25:39

#### 扩展欧几里得 模板

2016-08-04 22:12:26

#### 扩展欧几里德算法(附证明)

2015-10-24 23:45:49

#### 欧几里德和扩展欧几里德

2016-07-22 08:40:17

#### 欧几里德及扩展欧几里德

2018-03-24 19:35:17