Luogu P4549 裴蜀定理 / Min

思路

题目已经给出了正解。我们只需要将裴蜀定理推广到若干数的线性组合就可以做这道题了

要注意的是需要在输入的时候取一个绝对值。因为可能会有负数存在。我之前也写过裴蜀定理的证明,要看的话点这里

吐槽

第一次提交gcd写错了('汗

QwQ

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

inline int gcd(int x, int y) {
    return y ? gcd(y, x%y) : x;
}

int n;

int main() {
    scanf("%d", &n);
    int ans = 0, tmp;
    for(int i=1; i<=n; i++) {
        scanf("%d", &tmp);
        if(tmp < 0) tmp = -tmp;
        ans = gcd(ans, tmp);
    }
    printf("%d", ans);
}

猜你喜欢

转载自www.cnblogs.com/bljfy/p/9316822.html