【扩展欧几里得】Codevs 1200: [noip2012]同余方程


Description

  求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解。 

Input Description

  输入只有一行,包含两个正整数 a, b,用 一个 空格隔开。 

Output Description

  输出只有一行包含一个正整数x0,即最小正整数解,输入数据保证一定有解。


  裸的exgcd,不多讲了。。
  
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 
 6 typedef long long ll;
 7 
 8 using namespace std;
 9 
10 ll x,y;
11 
12 void exgcd(ll a,ll b)
13 {
14     if(b==0)
15     {
16         x=1;
17         y=0;
18         return;
19     }
20     exgcd(b,a%b);
21     ll 
22     sb=x;
23     x=y;
24     y=sb-(a/b*y);
25 }
26 
27 void solve(ll a,ll b)
28 {
29     exgcd(a,b);
30     printf("%lld",(x+b)%b);//最小正整数解
31 }
32 
33 int main()
34 {
35     ll a,b;
36     scanf("%lld%lld",&a,&b);
37     solve(a,b);
38     return 0;
39 }
View Code
原文地址:https://www.cnblogs.com/tuigou/p/4628349.html