-
Notifications
You must be signed in to change notification settings - Fork 0
/
1025. Divisor Game.c
92 lines (81 loc) · 1.63 KB
/
1025. Divisor Game.c
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
/** C */
bool findIt(int N,int ch)
{
int i=1;
if(N==1)
{
if(ch==0)
return false;
else
return true;
}
for(i;i<N;i++)
{
if(N%i==0)
{
return findIt(N-i,ch^1);
}
}
return false;
}
bool divisorGame(int N){
return findIt(N,0);
}
/** C++ */
class Solution {
public:
bool divisorGame(int N) {
return N % 2 == 0;
}
};
class Solution {
public:
bool divisorGame(int N) {
if(N%2)
return false;
return true;
}
};
/** Java */
class Solution {
private static int[] record = new int[1001];
static {
record[1] = -1;
record[2] = 1;
}
public boolean divisorGame(int N) {
if (N < 1 || N > 1000) return false;
if (record[N] != 0)
return record[N]==1;
for (int i = N/2; i>0; i--)
if (N % i == 0)
if (record[N-i] == -1 ||
(record[N-i] == 0 && !divisorGame(N-i))) {
record[N] = 1;
return true;
}
record[N] = -1;
return false;
}
}
class Solution {
public boolean divisorGame(int N) {
return N % 2 == 0;
}
}
/** Python */
class Solution(object):
def divisorGame(self, N):
"""
:type N: int
:rtype: bool
"""
return N % 2 == 0
class Solution(object):
def divisorGame(self, N):
count, x = 0, 1
while N > 1:
N, count = N - x, count + 1
if N == 1:
if count % 2 == 0: return False
else: return True