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/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md
+97-21
Original file line number
Diff line number
Diff line change
@@ -50,29 +50,107 @@
50
50
51
51
## Solutions
52
52
53
-
### Solution 1
53
+
### Solution 1: Mathematics
54
+
55
+
According to the problem description, in each move, the player will choose to move in a clockwise or counterclockwise direction and then pick a flower. Since Alice moves first, when $x + y$ is odd, Alice will definitely win the game.
56
+
57
+
Therefore, the number of flowers $x$ and $y$ meet the following conditions:
58
+
59
+
1. $x + y$ is odd;
60
+
2. $1 \le x \le n$;
61
+
3. $1 \le y \le m$.
62
+
63
+
If $x$ is odd, $y$ must be even. At this time, the number of values of $x$ is $\lceil \frac{n}{2} \rceil$, the number of values of $y$ is $\lfloor \frac{m}{2} \rfloor$, so the number of pairs that meet the conditions is $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor$.
64
+
65
+
If $x$ is even, $y$ must be odd. At this time, the number of values of $x$ is $\lfloor \frac{n}{2} \rfloor$, the number of values of $y$ is $\lceil \frac{m}{2} \rceil$, so the number of pairs that meet the conditions is $\lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$.
66
+
67
+
Therefore, the number of pairs that meet the conditions is $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$, which is $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$.
68
+
69
+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
54
70
55
71
<!-- tabs:start -->
56
72
57
73
```python
58
74
classSolution:
59
75
defflowerGame(self, n: int, m: int) -> int:
60
-
count = (n +1) //2
61
-
tol = (m +1) //2
62
-
ecount = n //2
63
-
etol = m //2
64
-
return count * etol + ecount * tol
76
+
a1 = (n +1) //2
77
+
b1 = (m +1) //2
78
+
a2 = n //2
79
+
b2 = m //2
80
+
return a1 * b2 + a2 * b1
81
+
```
65
82
83
+
```java
84
+
classSolution {
85
+
publiclongflowerGame(intn, intm) {
86
+
long a1 = (n +1) /2;
87
+
long b1 = (m +1) /2;
88
+
long a2 = n /2;
89
+
long b2 = m /2;
90
+
return a1 * b2 + a2 * b1;
91
+
}
92
+
}
93
+
```
94
+
95
+
```cpp
96
+
classSolution {
97
+
public:
98
+
long long flowerGame(int n, int m) {
99
+
long long a1 = (n + 1) / 2;
100
+
long long b1 = (m + 1) / 2;
101
+
long long a2 = n / 2;
102
+
long long b2 = m / 2;
103
+
return a1 * b2 + a2 * b1;
104
+
}
105
+
};
106
+
```
107
+
108
+
```go
109
+
func flowerGame(n int, m int) int64 {
110
+
a1, b1 := (n+1)/2, (m+1)/2
111
+
a2, b2 := n/2, m/2
112
+
return int64(a1*b2 + a2*b1)
113
+
}
114
+
```
115
+
116
+
```ts
117
+
function flowerGame(n:number, m:number):number {
118
+
const [a1, b1] = [(n+1) >>1, (m+1) >>1];
119
+
const [a2, b2] = [n>>1, m>>1];
120
+
returna1*b2+a2*b1;
121
+
}
122
+
```
123
+
124
+
<!-- tabs:end -->
125
+
126
+
### Solution 2: Mathematics (Optimized)
127
+
128
+
The result obtained from Solution 1 is $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$.
129
+
130
+
If both $n$ and $m$ are odd, then the result is $\frac{n + 1}{2} \times \frac{m - 1}{2} + \frac{n - 1}{2} \times \frac{m + 1}{2}$, which is $\frac{n \times m - 1}{2}$.
131
+
132
+
If both $n$ and $m$ are even, then the result is $\frac{n}{2} \times \frac{m}{2} + \frac{n}{2} \times \frac{m}{2}$, which is $\frac{n \times m}{2}$.
133
+
134
+
If $n$ is odd and $m$ is even, then the result is $\frac{n + 1}{2} \times \frac{m}{2} + \frac{n - 1}{2} \times \frac{m}{2}$, which is $\frac{n \times m}{2}$.
135
+
136
+
If $n$ is even and $m$ is odd, then the result is $\frac{n}{2} \times \frac{m - 1}{2} + \frac{n}{2} \times \frac{m + 1}{2}$, which is $\frac{n \times m}{2}$.
137
+
138
+
The above four cases can be combined into $\lfloor \frac{n \times m}{2} \rfloor$.
139
+
140
+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
0 commit comments