Luo Gu Shu Pei P4549 [template] Theorem

Topic
Learning blog

Theorem Content:

For any \ (a, b \ in Z \) and their greatest common divisor \ (D \) , on unknown \ (X \) and \ (Y \) is the linear equation variable \ (ax + by = c \ ) Solutions of integers \ ((x, y) \ ) if and only if \ (D | C \) , there are infinitely many solutions found. In particular, there must be an integer that the \ (ax + by = d \ ) established

inference:

\ (a, b \) coprime integers iff \ (x, y \) makes \ (= AX +. 1 by \) .

prove:

Set \ (gcd (a, b)
= d \) readily available: \ (D | A \) , \ (D | B \)
and \ (\ Because \) \ (X \ in the Z, Y \ in the Z \ )
then available: \ (d | AX + by \)
Like syndrome finished

About this question is to be extended to more than two variables variables
beg \ (n-1 \) times \ (gcd \) to
the code is simple home

#include <cstdio>
#include <iostream>
using namespace std;
int n, ans, x, y;
int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
    while(isdigit(ch)) {s = s * 10 + ch - '0'; ch = getchar();}
    return s * w;
}
int gcd(int x, int y) {
//  if(x < 0) x = -x;
//  if(y < 0) y = -y;
    return y == 0 ? x : gcd(y, x % y); 
}
int main() {
    n = read();
    x = read(), y = read();
    if(x < 0) x = -x;
    if(y < 0) y = -y;
    ans = gcd(x, y);
    for(int i = 1; i <= n - 2; i++) {
        y = read();
        if(y < 0) y = -y;
        ans = gcd(ans, y);
//      x = ans;
    }
    cout << ans << endl;
    return 0;
}

Thank you for watching, I wish good health!

Guess you like

Origin www.cnblogs.com/yanxiujie/p/11706679.html