forked from ckcz123/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.java
100 lines (86 loc) · 2.83 KB
/
A.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
91
92
93
94
95
96
97
98
99
100
import java.io.PrintStream;
import java.util.*;
/**
* KickStart 2017 Round G
* Problem A. Huge Numbers
*/
public class Main {
public String solve(Scanner scanner) {
long a=scanner.nextLong(), n=scanner.nextLong(), p=scanner.nextLong();
if (p==1) return "0";
if (a==1) return "1";
if (n<=4) return String.valueOf(calSmall(a,n,p));
HashMap<Long, Integer> primes=calPrimes(p);
HashMap<Long, Long> remains=new HashMap<>();
for (Map.Entry<Long, Integer> entry: primes.entrySet()) {
remains.put(entry.getKey(), calRemains(a, n, entry.getKey(), entry.getValue()));
}
for (long i=0;i<p;i++)
if (check(primes, remains, i))
return String.valueOf(i);
return "";
}
private HashMap<Long, Integer> calPrimes(long p) {
HashMap<Long, Integer> primes=new HashMap<>();
long v=p;
for (long i=2;i*i<=p;i++) {
int cnt=0;
while (v%i==0) {
cnt++; v/=i;
}
if (cnt>0) primes.put(i, cnt);
}
if (v!=1) primes.put(v, 1);
return primes;
}
private long calRemains(long a, long n, long p, long v) {
// a^(n!) % (p^v)
if (a%p==0) return 0;
// a^(v*(p-1)) === 1 (mod p^v)
long u=v*(p-1);
long tot=1;
for (int i=1;i<=n;i++) {
tot*=i; tot%=u;
}
long ans=1;
for (int i=0;i<tot;i++) {
ans*=a;
ans%=p;
}
return ans;
}
private long calSmall(long a, long n, long p) {
long tot=1, ans=1;
for (int i=1;i<=n;i++) tot*=i;
for (int i=0;i<tot;i++) {
ans*=a;ans%=p;
}
return ans;
}
private boolean check(HashMap<Long, Integer> primes, HashMap<Long, Long> remains, long candidate) {
for (Map.Entry<Long, Integer> entry: primes.entrySet()) {
long p=entry.getKey(), ans=1;
for (int i=0;i<entry.getValue();i++) ans*=p;
if (candidate%ans!=remains.get(p))
return false;
}
return true;
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=Integer.parseInt(scanner.nextLine());
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
try {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
catch (Throwable e) {
System.err.println("ERROR in case #"+t);
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}