题解 P4549 【裴蜀定理 / Min】
欢迎到家光顾我的博客
引言
这个题只要带着眼睛都知道要用到裴蜀定理(又叫做贝祖定理)。那么这个定理讲了啥呢?怎么证明?
裴蜀定理内容
又因为
所以
显然要使得之前的式子成立,则必须满足
又因为
所以
因此,证得该定理成立
针对这道题
上述裴蜀定理针对的是两个变量。那么我们很自然的就想到这样的定理能否推广到多个变量呢?显然可以,证明方法同上。
那这个题不就是推广后的定理的裸题吗QAQ。我们只需要对这所有的数字求一个
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gcd(int x, int y) {
return y ? gcd(y, x%y) : x;
}
int n;
int main() {
scanf("%d", &n);
int ans = 0, tmp;
for(int i=1; i<=n; i++) {
scanf("%d", &tmp);
if(tmp < 0) tmp = -tmp;
ans = gcd(ans, tmp);
}
printf("%d", ans);
}