BZOJ 1441 Min (裴蜀定理)

Description

给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小

Input

第一行给出数字N,代表有N个数 下面一行给出N个数

Output

S的最小值

Sample Input

2
4059 -1782

Sample Output

99
 
解题思路
根据裴蜀定理可得,一定存在一组解满足$ax+by=gcd(x,y)$,这个也可以扩展到多个的情况,最后的答案其实就是$gcd(a[0],a[1],...,a[n])$。
 
 
#include<iostream>
#include<cstdio>
#include<algorithm>
#define czq namespace
using czq std;
typedef long long LL;
inline LL rd(){
    LL x=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch))  {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x;
}
LL ans,n;
int main(){
    n=rd();ans=rd();n--;
    while(n--) ans=__gcd(ans,rd());cout<<ans; 
    return 0;
}
View Code

 

 
posted @ 2018-10-08 09:54  Monster_Qi  阅读(168)  评论(0编辑  收藏  举报