forked from lewin/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fact0.java
90 lines (79 loc) · 2.97 KB
/
fact0.java
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.security.SecureRandom;
public class fact0
{
private final static BigInteger ZERO = new BigInteger("0");
private final static BigInteger ONE = new BigInteger("1");
private final static BigInteger TWO = new BigInteger("2");
private final static SecureRandom random = new SecureRandom();
private static PrintWriter out;
private static Map <BigInteger, Integer> mp;
public static void main (String [] args) throws IOException {
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
out = new PrintWriter (System.out, true);
while (true) {
BigInteger n = new BigInteger (in.readLine ());
if (n.compareTo (ZERO) == 0) break;
mp = new HashMap <BigInteger, Integer> ();
factor (n);
Set <Map.Entry <BigInteger, Integer>> e = mp.entrySet ();
Iterator exp = e.iterator ();
int j = 0; Key [] keys = new Key [e.size()];
while (exp.hasNext ()) {
Map.Entry a = (Map.Entry) exp.next ();
keys [j++] = new Key (a.getKey ().toString (), a.getValue ().toString ());
}
Arrays.sort (keys, 0, j);
for (int i = 0; i < j - 1; i++)
out.print(keys[i] + " x ");
out.println (keys [j - 1]);
}
}
private static BigInteger rho(BigInteger N) {
BigInteger divisor;
BigInteger c = new BigInteger(N.bitLength(), random);
BigInteger x = new BigInteger(N.bitLength(), random);
BigInteger xx = x;
// check divisibility by 2
if (N.mod(TWO).compareTo(ZERO) == 0) return TWO;
do {
x = x.multiply(x).mod(N).add(c).mod(N);
xx = xx.multiply(xx).mod(N).add(c).mod(N);
xx = xx.multiply(xx).mod(N).add(c).mod(N);
divisor = x.subtract(xx).gcd(N);
} while((divisor.compareTo(ONE)) == 0);
return divisor;
}
private static void factor(BigInteger N) {
if (N.compareTo(ONE) == 0) return;
if (N.isProbablePrime(5)) {
Integer p = mp.remove (N);
if (p == null) p = new Integer (0);
p = (Integer)(p.intValue()+1);
mp.put (N, p);
return;
}
BigInteger divisor = rho(N);
factor(divisor);
factor(N.divide(divisor));
}
static class Key implements Comparable <Key> {
public String factor, exp;
public Key (String _factor, String _exp) {
factor = _factor;
exp = _exp;
}
public int compareTo (Key o) {
int p = factor.length ();
int q = o.factor.length ();
if (p > q) return 1;
if (q > p) return -1;
return factor.compareTo (o.factor);
}
public String toString () {
return factor + "^" + exp;
}
}
}