裴蜀定理內(nèi)容:\(ax+by=c\), \(x \in Z^?\), \(y \in Z^?\) 成立的充要條件是 \(gcd?(a, b) ∣ c\)。\(Z^*\) 表示正整數(shù)集。
luogu:https://www.luogu.com.cn/problem/P4549
給定一個(gè)序列 \(a\),找到一個(gè)序列 \(x\),使得 \(\sum_{i = 1}^n a_ix_i\) 最小。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL n, a, ans;
LL gcd(LL a, LL b){
return b ? gcd(b, a % b) : a;
}
int main(){
cin >> n;
for (int i = 0; i < n; i ++ ){
cin >> a;
if (a < 0) a = -a;
ans = gcd(ans, a);
}
cout << ans << "\n";
return 0;
}
作者:Hamine
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但必須給出原文鏈接,并保留此段聲明,否則保留追究法律責(zé)任的權(quán)利。
浙公網(wǎng)安備 33010602011771號(hào)