跳到正文

个人记录

P1082 [NOIP 2012 提高组] 同余方程 (拓展欧几里得)

yqzx0307约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年2月7日
收录
2026年7月3日
更新
2026年11月2日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;
 #define int long long
 int exgcd(int a,int b,int &x,int &y)
 {
     if(b==0)
     {x=1,y=0;
         return a;
     }int d=exgcd(b,a%b,y,x);
     y-=a/b*x;
     return d;
 }
signed main()
{
    int a,b,x,y,c=1;
    scanf("%lld%lld",&a,&b);
    int d=exgcd(a,b,x,y);
    int xs=c/d;
    x*=xs,y*=xs;
    int d1=b/d,d2=-a/d;
    x=(x%d1+d1)%d1;
    printf("%lld",x);
    return 0;
}

https://www.luogu.com.cn/problem/P1082

原文