题目传送门
裴蜀定理也叫做(贝祖定理)
定理内容:如果方程 ax + by == c,如果x,y的解为整数,那么c一定是gcd(a,b)的倍数。//gcd(a,b)为a,b的最大公约数
博主的粗略证明(不知道对不对哈。。。。)
当x,y都等于1的时候:
a%gcd(a,b) == 0,b%gcd(a,b) == 0.那么(a+b)%gcd(a,b) == 0,而c == a+b;
当x,y任意取时,同理可推;
当方程组为多元时,可推得一样成立(这样,那样,证明成功。。。。嘻嘻嘻)。
更新:
看这个证明可能要好懂点
例:
2 * x + 4 * y = c
那么c无论如何都是偶数,换句话来说,c一定是2的倍数2 = gcd(2,4)
3 * x + 9 * y = c
那么同理c一定是3的倍数,因为上述等于 : 3 * x + 3 * 3 * y = c
3 * (x + 3 * y) = c,外面乘了个3,那么c再怎么都是3的倍数,3 = gcd(3,9)
那么这个定理有什么用呢。当x,y为整数解的时候,c一定是gcd(a,b)的倍数,那么我们就可以求得当为整数解的时候,c的最小值肯定就是gcd(a,b)//最大公约数的1倍。
题目链接AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const int mod = 1e9 + 7;
const int II = 3.1415926535;
typedef long long ll;
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin >> n;
ll arr[n+5];
for(int i = 0;i < n;i++)
{
ll x;
cin >> x;
arr[i] = abs(x);
}
ll ans = arr[0];
for(int i = 1;i < n;i++)
ans = __gcd(ans,arr[i]);
cout << ans << endl;
}