【BZOJ-1441】Min 裴蜀定理 + 最大公约数
1441: Min
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 471 Solved: 314
[Submit][Status][Discuss]
Description
给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小
Input
第一行给出数字N,代表有N个数下面一行给出N个数
Output
S的最小值
Sample Input
2
4059 -1782
4059 -1782
Sample Output
99
HINT
Source
Solution
裴蜀定理推广到N个数
具体证明可以类似2个数证明
Code
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> using namespace std; int n,ans; int Gcd(int a,int b) {if (b==0) return a; return Gcd(b,a%b);} int main() { scanf("%d",&n); for (int x,i=1; i<=n; i++) scanf("%d",&x),ans=Gcd(ans,x); printf("%d",abs(ans)); return 0; }
——It's a lonely path. Don't make it any lonelier than it has to be.