zoukankan      html  css  js  c++  java
  • 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 有整数解 x y 的条件是 c 是 gcd(a,b) | c , 所以 c 为最小正整数的话就是要求 c 是a b 的最大公约数。

    这个定理对于多个变量来说同样使用。可以看成两个变量的不断累加。

    但是要注意输入的数可能是负数,此时直接取反就可以了,对gcd没有影响。

    代码

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string.h>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<deque>
    #include<map>
    #include<iostream>
    using namespace std;
    typedef long long  LL;
    const double pi=acos(-1.0);
    const double e=exp(1);
    const int N = 998244353;
    
    int gcd(int a, int b)
    {
        int mid;
        while(b)
        {
            mid = a;
            a = b;
            b = mid % b;
        }
        return a;
    }
    
    int main()
    {
        int i,p,j,n,t;
        scanf("%d",&n);
        scanf("%d",&p);
        if(p < 0)
            p = -p;
    
        for(i = 1; i < n; i++)
        {
            scanf("%d",&j);
            if(j < 0)
                j = -j;
            p = gcd(p,j);
        }
    
        printf("%d
    ",p);
        return 0;
    }
    View Code
  • 相关阅读:
    win10 uwp 弹起键盘不隐藏界面元素
    win10 uwp 存放网络图片到本地
    win10 uwp 存放网络图片到本地
    sublime Text 正则替换
    sublime Text 正则替换
    win10 uwp 绘图 Line 控件使用
    win10 uwp 绘图 Line 控件使用
    AJAX 是什么?
    什么是 PHP SimpleXML?
    PHP XML DOM:DOM 是什么?
  • 原文地址:https://www.cnblogs.com/daybreaking/p/10726492.html
Copyright © 2011-2022 走看看