1 定理一:如果d = gcd(a, b),则必能找到正的或负的整数x和y,使 d = a*x+ b*y。
2
3 定理二:若gcd(a, b) = 1,则方程ax ≡ c (mod b)在[0, b-1]上有唯一解。
4
5 定理三:若gcd(a, b) = d,则方程ax ≡ c (mod b)在[0, b/d - 1]上有唯一解。
6
7 对于ax+by=1; 即ax=1(mod b) 当且仅当gcd(a,b)!=1 的时候,无解!
8
9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <string>
13 #include <utility>
14 #include <stack>
15 #include <map>
16 #include <queue>
17 #include <vector>
18 #include <cmath>
19 #include <iostream>
20 #include <algorithm>
21 #define pi acos(-1.0)
22 #define e 2.718
23 #define lowbit(x) (x&(-x))
24 using namespace std;
25 typedef unsigned long long ull;
26 typedef long long ll;
27 int t,a,b;
28 int egcd(int a,int b,int &x,int &y)
29 {
30 int d=a;
31 if(!b)
32 {
33 x=1,y=0;
34 }
35 else
36 {
37 d=egcd(b,a%b,y,x);
38 y-=x*(a/b);
39 }
40 return d;
41 }
42 int solve(int a,int b)
43 { int d,x,y;
44 d=egcd(a,b,x,y);
45 if(d!=1)
46 {
47 return -1;
48 }
49 else
50 {
51 while(x<=0)
52 {
53 x+=b/d;
54 }
55 }
56 return x;
57 }
58 int main()
59 {
60 scanf("%d",&t);
61 while(t--)
62 {
63 scanf("%d %d",&a,&b);
64 if(solve(a,b)==-1)
65 {
66 printf("Not Exist\n");
67 }
68 else
69 {
70 printf("%d\n",solve(a,b));
71 }
72 }
73 return 0;
74 }