You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2000-2099/2029.Stone Game IX/README_EN.md
+93-55Lines changed: 93 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,19 @@ Alice loses the game because the sum of the removed stones (15) is divisible by
77
77
78
78
<!-- solution:start -->
79
79
80
-
### Solution 1
80
+
### Solution 1: Greedy + Case Discussion
81
+
82
+
Since the player's goal is to ensure the total value of the removed stones is not divisible by 3ドル,ドル we only need to consider the remainder of each stone's value when divided by 3ドル$.
83
+
84
+
We use an array $\textit{cnt}$ of length 3ドル$ to maintain the count of the current remaining stones' values modulo 3ドル,ドル where $\textit{cnt}[0]$ represents the count of stones with a remainder of 0ドル,ドル and $\textit{cnt}[1]$ and $\textit{cnt}[2]$ respectively represent the counts of stones with remainders of 1ドル$ and 2ドル$.
85
+
86
+
In the first round, Alice cannot remove stones with a remainder of 0ドル,ドル as this would make the total value of the removed stones divisible by 3ドル$. Therefore, Alice can only remove stones with a remainder of 1ドル$ or 2ドル$.
87
+
88
+
First, let's consider the case where Alice removes a stone with a remainder of 1ドル$. If Alice removes a stone with a remainder of 1ドル,ドル the remainder of the total value of stones 0ドル$ against 3ドル$ will not change, so stones with a value remainder of 0ドル$ can be removed in any round, which we will not consider for now. Thus, Bob can only remove stones with a remainder of 1ドル,ドル followed by Alice removing stones with a remainder of 2ドル,ドル and so on, in the sequence 1,ドル 1, 2, 1, 2, \ldots$. In this scenario, if the final round is odd and there are still remaining stones, then Alice wins; otherwise, Bob wins.
89
+
90
+
For the case where Alice removes a stone with a remainder of 2ドル$ in the first round, we can draw a similar conclusion.
91
+
92
+
The time complexity is $O(n),ドル where $n$ is the length of the array $\textit{stones}$. The space complexity is $O(1)$.
81
93
82
94
<!-- tabs:start -->
83
95
@@ -86,47 +98,46 @@ Alice loses the game because the sum of the removed stones (15) is divisible by
0 commit comments