forked from Sandip-Jana/Algorithms-ACM-ICPC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModular_Linear_Equation_Solver.java
More file actions
67 lines (67 loc) · 1.57 KB
/
Modular_Linear_Equation_Solver.java
File metadata and controls
67 lines (67 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.*;
import java.util.*;
public class Modular_Linear_Equation_Solver // solve a * x = (b mod n) : solve for x
{
BufferedReader in;
PrintWriter ob;
StringTokenizer st;
// a*x + n*y = b
// a*x = b (mod n)
// solve for x
// there are only d distinct values of x such that d=gcd(a,n) from 0 to d-1;
public static void main(String[] args) throws IOException {
new Modular_Linear_Equation_Solver().run();
}
void run() throws IOException {
in=new BufferedReader(new InputStreamReader(System.in));
ob=new PrintWriter(System.out);
solve();
ob.flush();
}
void solve() throws IOException {
int a=ni();
int b=ni();
int n=ni();
Triplet p = extended_euclid(a , n , 0 , 0);
ob.println(p.x+" "+p.y+" "+p.z);
int gcd=p.z;
if(b%gcd==0) {
int x=(p.x*(b/gcd))%n;
for(int i=0 ; i<=gcd-1 ; i++) {
ob.print(((n+x+(i*(n/gcd)))%n)+ " ");
}
ob.println();
}
else
ob.println("No Solution Exists");
}
Triplet extended_euclid(int a, int b, int x , int y) {
if(b==0) {
x=1;
y=0;
return new Triplet(x,y,a);
}
int x1=0, y1=0;
Triplet p = extended_euclid( b , a%b , x1 , y1 ); // gcd( b , a%b ) = b*x + (a-(a/b)*b)*y = a*y + b*(x - (a/b) * y);
x=p.y;
y=p.x-((a/b)*(p.y));
return new Triplet(x,y,p.z);
}
int ni() throws IOException {
return Integer.parseInt(nextToken());
}
String nextToken() throws IOException {
if(st==null || !st.hasMoreTokens())
st=new StringTokenizer(in.readLine());
return st.nextToken();
}
}
class Triplet
{
int x,y,z;
public Triplet(int x, int y, int z) {
this.x=x;
this.y=y;
this.z=z;
}
}