洛谷 P4549 【模板】裴蜀定理

题目

题目描述

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

输入格式

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

输出格式

S的最小值

输入输出样例

输入 #1
2
4059 -1782
输出 #1
99

说明/提示

对于100%的数据,1 \le n \le 201n20,|x_i| \le 100000xi100000

 

分析

  • 首先,肯定是用这个定理啦

  • 这个定理是 ax+by=c c|gcd(a,b)
  • 所以两个数的最小整数解肯定是两数的gcd
  • 那么多个数
  • 就是多个数的gcd啦

 

代码

 

 

 1 #include<iostream>
 2 using namespace std;
 3 int gcd(int a,int b)
 4 {
 5     if (b==0) return a;
 6     gcd(b,a%b);
 7 }
 8 int main ()
 9 {
10     int n,ans=0;
11     cin>>n;
12     for (int i=1,x;i<=n;i++)
13     {
14         cin>>x;
15         if (x<0)
16           x=-x;
17         ans=gcd(ans,x);
18     } 
19     cout<<ans;
20 }

 

posted @ 2019-08-08 08:06  Melted_czj  阅读(185)  评论(0编辑  收藏  举报
body { background-color:whitesmoke; } // 修改背景颜色为半透明 #home,#sideBarMain>div,#blog-sidecolumn>div>div,.catListView{ background-color:rgba(255,255,255,0); } // 修改其他边框的颜色