洛谷P4549 【模板】裴蜀定理

题目
定理内容:

对于任何\(a,b \in Z\)和他们的最大公约数\(d\),关于未知数\(x\)\(y\)的线性不定方程\(ax+by=c\)有整数解\((x,y)\)当且仅当\(d|c\),可知有无穷多组解。特别的,一定存在整数使\(ax+by=d\)成立

推论:

\(a,b\)互质的充要条件是存在整数\(x,y\)使\(ax+by=1\)

证明:

\(gcd(a,b)=d\)
易得:\(d|a\)\(d|b\)
\(\because\) \(x\in Z,y\in Z\)
继而可得:\(d|ax+by\)
好像证完了

关于本题就是将两个变量推广到了多个变量
\(n-1\)\(gcd\)即可
代码也是简洁到家

#include <cstdio>
#include <iostream>
using namespace std;
int n, ans, x, y;
int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
    while(isdigit(ch)) {s = s * 10 + ch - '0'; ch = getchar();}
    return s * w;
}
int gcd(int x, int y) {
//  if(x < 0) x = -x;
//  if(y < 0) y = -y;
    return y == 0 ? x : gcd(y, x % y); 
}
int main() {
    n = read();
    x = read(), y = read();
    if(x < 0) x = -x;
    if(y < 0) y = -y;
    ans = gcd(x, y);
    for(int i = 1; i <= n - 2; i++) {
        y = read();
        if(y < 0) y = -y;
        ans = gcd(ans, y);
//      x = ans;
    }
    cout << ans << endl;
    return 0;
}

谢谢收看,祝身体健康!

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-18 22:30
我看都是谁在卷前端!
秋盈丶:搜了下,20人的公司能收到2000份招呼?真有这么夸张吗
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着接:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务