Simple proof of Pei Shu's theorem and its extension (Luogu P4549)

Title Portal

Pei Shu's theorem The
so-called Pei Shu's theorem is that for two positive integers x, y, if ax + by = c, then c% gcd (x, y) == 0.

Proof
Let s = gcd (x, y), then x% s == 0, y% s == 0, of course there is ax% s == 0, by% s == 0, so c% s == 0.

If it is three positive integers x, y, z, then ax + by + dz = c, the same reason c% gcd (x, y, z) == 0.

Generalization
From two numbers to multiple numbers, the final result must be a multiple of the gcd of these multiple numbers.

Tips
There is a negative number entered in the link question. You need to change it to a positive integer and find gcd.

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf=0x7fffffff;
const int mod=1e9+7;
const int eps=1e-6;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
//#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
int a[N];
signed main()
{
    IOS;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        a[i]=abs(a[i]);
    }
    int res=a[1];
    for(int i=2;i<=n;i++)
    {
        res=__gcd(res,a[i]);
    }
    cout<<res<<endl;
}
Published 93 original articles · won praise 9 · views 4203

Guess you like

Origin blog.csdn.net/Joker_He/article/details/104706520