Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 45b509f

Browse files
🐱(dynamic): 1025. 除数博弈
1 parent fad7de8 commit 45b509f

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

‎docs/algorithm/dynamic/README.md‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,4 +1655,32 @@ class Solution:
16551655
return self.tribonacci(n - 3) + self.tribonacci(n - 2) + self.tribonacci(n - 1)
16561656
```
16571657

1658-
<!-- tabs:end -->
1658+
<!-- tabs:end -->
1659+
1660+
## 1025. 除数博弈
1661+
1662+
[原题链接](https://leetcode-cn.com/problems/divisor-game/)
1663+
1664+
### 思路
1665+
1666+
`N = 1` 时Alice 会输,当 `N = 2` 时 Alice 会赢......
1667+
1668+
`dp(N)` 代表 `N` 时先手的输赢。
1669+
1670+
`N` 中找到约数 `j`,若 `dp(N - j) == False`(Bob 输) 则说明 Alice 获胜。
1671+
1672+
```python
1673+
class Solution:
1674+
def divisorGame(self, N: int) -> bool:
1675+
dp = [False for _ in range(N + 1)]
1676+
if N <= 1:
1677+
return False
1678+
dp[1] = False
1679+
dp[2] = True
1680+
for i in range(3, N + 1):
1681+
for j in range(1, i // 2):
1682+
# i 的约数是否存在 dp[j] 为 False,此时 Alice 获胜
1683+
if i % j == 0 and dp[i - j] is False:
1684+
dp[i] = True
1685+
return dp[N]
1686+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /