-
Notifications
You must be signed in to change notification settings - Fork 7
/
1025.cpp
41 lines (34 loc) · 832 Bytes
/
1025.cpp
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
// trick solution
// Time : O(1)
class Solution {
public:
bool divisorGame(int n) {
return n % 2 == 0;
}
};
// solution using dynamic programming
// Time : O(n^2)
class Solution {
public:
vector<int>dp = vector<int>(1001, -1);
int solve(int n){
if(n == 1)
return 0;
if(dp[n] != -1)
return dp[n];
else{
for(int i=1; i*i <= n; i++){
if(n % i == 0){
if(solve(n - i) == 0)
return dp[n] = 1;
if((i != 1) && solve(n - (n/i)) == 0)
return dp[n] = 1;
}
}
return dp[n] = 0;
}
}
bool divisorGame(int n) {
return solve(n);
}
};