Skip to content

Commit 561351d

Browse files
committed
feat: add solutions to lc problem: No.1025
No.1025.Divisor Game
1 parent c6328f0 commit 561351d

File tree

6 files changed

+82
-21
lines changed

6 files changed

+82
-21
lines changed

solution/1000-1099/1025.Divisor Game/README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,66 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:数学归纳法**
56+
57+
- $n=1$,先手败
58+
- $n=2$,先手拿 $1$,剩下 $1$,后手败,先手胜
59+
- $n=3$,先手拿 $1$,剩下 $2$,后手胜,先手败
60+
- $n=4$,先手拿 $1$,剩下 $3$,后手败,先手胜
61+
- ...
62+
63+
猜想,$n$ 为奇数时,先手败;$n$ 为偶数时,先手胜。
64+
65+
证明:
66+
67+
1. $n=1$ 或 $n=2$ 时,结论成立;
68+
1. $n>2$ 时,假设 $n \le k$ 时,该结论成立,则 $n=k+1$ 时:
69+
- 若 $k+1$ 为奇数,$x$ 是 $k+1$ 的因数,$x$ 只可能是奇数,那么 $k+1-x$ 为偶数,后手胜,先手败;
70+
- 若 $k+1$ 为偶数,$x$ 既可以是奇数 $1$,也可以是偶数,$x$ 取奇数,那么 $k+1-x$ 为奇数,后手败,先手胜。
71+
72+
综上,$n$ 为奇数时,先手败;$n$ 为偶数时,先手胜。结论正确。
73+
5574
<!-- tabs:start -->
5675

5776
### **Python3**
5877

5978
<!-- 这里可写当前语言的特殊实现逻辑 -->
6079

6180
```python
62-
81+
class Solution:
82+
def divisorGame(self, n: int) -> bool:
83+
return n % 2 == 0
6384
```
6485

6586
### **Java**
6687

6788
<!-- 这里可写当前语言的特殊实现逻辑 -->
6889

6990
```java
91+
class Solution {
92+
public boolean divisorGame(int n) {
93+
return n % 2 == 0;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
bool divisorGame(int n) {
104+
return n % 2 == 0;
105+
}
106+
};
107+
```
108+
109+
### **Go**
70110
111+
```go
112+
func divisorGame(n int) bool {
113+
return n%2 == 0
114+
}
71115
```
72116

73117
### **...**

solution/1000-1099/1025.Divisor Game/README_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,38 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def divisorGame(self, n: int) -> bool:
53+
return n % 2 == 0
5254
```
5355

5456
### **Java**
5557

5658
```java
59+
class Solution {
60+
public boolean divisorGame(int n) {
61+
return n % 2 == 0;
62+
}
63+
}
64+
```
65+
66+
### **C++**
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
bool divisorGame(int n) {
72+
return n % 2 == 0;
73+
}
74+
};
75+
```
76+
77+
### **Go**
5778
79+
```go
80+
func divisorGame(n int) bool {
81+
return n%2 == 0
82+
}
5883
```
5984

6085
### **...**
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
class Solution {
2-
unordered_map<int, bool> mem;
32
public:
4-
bool divisorGame(int N) {
5-
if (N == 1) return false;
6-
if (mem.count(N)) {
7-
return mem[N];
8-
}
9-
for (int i = 1; i < N; ++i) {
10-
if (N % i == 0) {
11-
if (divisorGame(N-i) == false) {
12-
mem[N] = true;
13-
return true;
14-
}
15-
}
16-
}
17-
mem[N] = false;
18-
return false;
3+
bool divisorGame(int n) {
4+
return n % 2 == 0;
195
}
206
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func divisorGame(n int) bool {
2+
return n%2 == 0
3+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution {
2-
public boolean divisorGame(int N) {
3-
return N % 2 == 0;
2+
public boolean divisorGame(int n) {
3+
return n % 2 == 0;
44
}
5-
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def divisorGame(self, n: int) -> bool:
3+
return n % 2 == 0

0 commit comments

Comments
 (0)