-
Notifications
You must be signed in to change notification settings - Fork 34
/
C.java
68 lines (60 loc) · 1.95 KB
/
C.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
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* APAC 2016 Round B Problem C: gNumbers
* Check README.md for explanation.
*/
public class Main {
private String solve(Scanner scanner) {
long n=scanner.nextLong(), val=n;
ArrayList<Long> arrayList=new ArrayList<>();
for (long p=2;p<=Math.sqrt(n);p+=(p==2?1:2)) {
if (n%p==0) {
long v=1;
while (n%p==0) {
n/=p;
v*=p;
}
arrayList.add(v);
}
}
if (n!=1) arrayList.add(n);
return win(val, arrayList, new HashMap<>())?"Laurence":"Seymour";
}
private boolean win(long n, ArrayList<Long> list, HashMap<Long, Boolean> map) {
if (map.containsKey(n)) return map.get(n);
if (isGNumber(n)) {
map.put(n, false);
return false;
}
for (long v: list) {
if (n%v==0 && !win(n/v, list, map)) {
map.put(n, true);
return true;
}
}
map.put(n, false);
return false;
}
private boolean isGNumber(long x) {
char[] chars=String.valueOf(x).toCharArray();
int sum=0;
for (char c: chars) sum+=c-'0';
for (int i=2;i<=Math.sqrt(sum);i++)
if (sum%i==0) 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=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}