diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md index 690cc655fceda..cc79cbab6c629 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md @@ -54,29 +54,107 @@ ## 解法 -### 方法一 +### 方法一:数学 + +根据题目描述,每一次行动,玩家都会选择顺时针或者逆时针方向,然后摘一朵鲜花。由于 Alice 先行动,因此当 $x + y$ 为奇数时,Alice 一定会赢得游戏。 + +因此,鲜花数目 $x$ 和 $y$ 满足以下条件: + +1. $x + y$ 为奇数; +2. $1 \le x \le n$; +3. $1 \le y \le m$。 + +若 $x$ 为奇数,$y$ 一定为偶数。此时 $x$ 的取值个数为 $\lceil \frac{n}{2} \rceil$,$y$ 的取值个数为 $\lfloor \frac{m}{2} \rfloor$,因此满足条件的数对个数为 $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor$。 + +若 $x$ 为偶数,$y$ 一定为奇数。此时 $x$ 的取值个数为 $\lfloor \frac{n}{2} \rfloor$,$y$ 的取值个数为 $\lceil \frac{m}{2} \rceil$,因此满足条件的数对个数为 $\lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$。 + +因此,满足条件的数对个数为 $\lceil \frac{n}{2} \rceil \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lceil \frac{m}{2} \rceil$,即 $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 ```python class Solution: def flowerGame(self, n: int, m: int) -> int: - count = (n + 1) // 2 - tol = (m + 1) // 2 - ecount = n // 2 - etol = m // 2 - return count * etol + ecount * tol + a1 = (n + 1) // 2 + b1 = (m + 1) // 2 + a2 = n // 2 + b2 = m // 2 + return a1 * b2 + a2 * b1 +``` +```java +class Solution { + public long flowerGame(int n, int m) { + long a1 = (n + 1) / 2; + long b1 = (m + 1) / 2; + long a2 = n / 2; + long b2 = m / 2; + return a1 * b2 + a2 * b1; + } +} +``` + +```cpp +class Solution { +public: + long long flowerGame(int n, int m) { + long long a1 = (n + 1) / 2; + long long b1 = (m + 1) / 2; + long long a2 = n / 2; + long long b2 = m / 2; + return a1 * b2 + a2 * b1; + } +}; +``` + +```go +func flowerGame(n int, m int) int64 { + a1, b1 := (n+1)/2, (m+1)/2 + a2, b2 := n/2, m/2 + return int64(a1*b2 + a2*b1) +} +``` + +```ts +function flowerGame(n: number, m: number): number { + const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1]; + const [a2, b2] = [n >> 1, m >> 1]; + return a1 * b2 + a2 * b1; +} +``` + + + +### 方法二:数学(优化) + +方法一得出的结果为 $\lfloor \frac{n + 1}{2} \rfloor \times \lfloor \frac{m}{2} \rfloor + \lfloor \frac{n}{2} \rfloor \times \lfloor \frac{m + 1}{2} \rfloor$。 + +如果 $n$ 和 $m$ 都是奇数,那么结果为 $\frac{n + 1}{2} \times \frac{m - 1}{2} + \frac{n - 1}{2} \times \frac{m + 1}{2}$,即 $\frac{n \times m - 1}{2}$。 + +如果 $n$ 和 $m$ 都是偶数,那么结果为 $\frac{n}{2} \times \frac{m}{2} + \frac{n}{2} \times \frac{m}{2}$,即 $\frac{n \times m}{2}$。 + +如果 $n$ 是奇数,且 $m$ 是偶数,那么结果为 $\frac{n + 1}{2} \times \frac{m}{2} + \frac{n - 1}{2} \times \frac{m}{2}$,即 $\frac{n \times m}{2}$。 + +如果 $n$ 是偶数,且 $m$ 是奇数,那么结果为 $\frac{n}{2} \times \frac{m - 1}{2} + \frac{n}{2} \times \frac{m + 1}{2}$,即 $\frac{n \times m}{2}$。 + +上面四种情况可以合并为 $\lfloor \frac{n \times m}{2} \rfloor$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 + + + +```python +class Solution: + def flowerGame(self, n: int, m: int) -> int: + return (n * m) // 2 ``` ```java class Solution { public long flowerGame(int n, int m) { - long count = (n + 1) / 2; - long tol = (m + 1) / 2; - long ecount = n / 2; - long etol = m / 2; - return (count * etol + ecount * tol); + return ((long) n * m) / 2; } } ``` @@ -85,22 +163,20 @@ class Solution { class Solution { public: long long flowerGame(int n, int m) { - long long count = (n + 1) / 2; - long long tol = (m + 1) / 2; - long long ecount = n / 2; - long long etol = m / 2; - return (count * etol + ecount * tol); + return ((long long) n * m) / 2; } }; ``` ```go func flowerGame(n int, m int) int64 { - count := int64((n + 1) / 2) - tol := int64((m + 1) / 2) - ecount := int64(n / 2) - etol := int64(m / 2) - return count*etol + ecount*tol + return int64((n * m) / 2) +} +``` + +```ts +function flowerGame(n: number, m: number): number { + return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n); } ``` diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md index 66a95ad5c5659..dd61ba407fe6a 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md @@ -50,29 +50,107 @@ ## Solutions -### Solution 1 +### Solution 1: Mathematics + +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. + +Therefore, the number of flowers $x$ and $y$ meet the following conditions: + +1. $x + y$ is odd; +2. $1 \le x \le n$; +3. $1 \le y \le m$. + +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$. + +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$. + +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$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. ```python class Solution: def flowerGame(self, n: int, m: int) -> int: - count = (n + 1) // 2 - tol = (m + 1) // 2 - ecount = n // 2 - etol = m // 2 - return count * etol + ecount * tol + a1 = (n + 1) // 2 + b1 = (m + 1) // 2 + a2 = n // 2 + b2 = m // 2 + return a1 * b2 + a2 * b1 +``` +```java +class Solution { + public long flowerGame(int n, int m) { + long a1 = (n + 1) / 2; + long b1 = (m + 1) / 2; + long a2 = n / 2; + long b2 = m / 2; + return a1 * b2 + a2 * b1; + } +} +``` + +```cpp +class Solution { +public: + long long flowerGame(int n, int m) { + long long a1 = (n + 1) / 2; + long long b1 = (m + 1) / 2; + long long a2 = n / 2; + long long b2 = m / 2; + return a1 * b2 + a2 * b1; + } +}; +``` + +```go +func flowerGame(n int, m int) int64 { + a1, b1 := (n+1)/2, (m+1)/2 + a2, b2 := n/2, m/2 + return int64(a1*b2 + a2*b1) +} +``` + +```ts +function flowerGame(n: number, m: number): number { + const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1]; + const [a2, b2] = [n >> 1, m >> 1]; + return a1 * b2 + a2 * b1; +} +``` + + + +### Solution 2: Mathematics (Optimized) + +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$. + +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}$. + +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}$. + +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}$. + +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}$. + +The above four cases can be combined into $\lfloor \frac{n \times m}{2} \rfloor$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. + + + +```python +class Solution: + def flowerGame(self, n: int, m: int) -> int: + return (n * m) // 2 ``` ```java class Solution { public long flowerGame(int n, int m) { - long count = (n + 1) / 2; - long tol = (m + 1) / 2; - long ecount = n / 2; - long etol = m / 2; - return (count * etol + ecount * tol); + return ((long) n * m) / 2; } } ``` @@ -81,22 +159,20 @@ class Solution { class Solution { public: long long flowerGame(int n, int m) { - long long count = (n + 1) / 2; - long long tol = (m + 1) / 2; - long long ecount = n / 2; - long long etol = m / 2; - return (count * etol + ecount * tol); + return ((long long) n * m) / 2; } }; ``` ```go func flowerGame(n int, m int) int64 { - count := int64((n + 1) / 2) - tol := int64((m + 1) / 2) - ecount := int64(n / 2) - etol := int64(m / 2) - return count*etol + ecount*tol + return int64((n * m) / 2) +} +``` + +```ts +function flowerGame(n: number, m: number): number { + return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n); } ``` diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.cpp b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.cpp index 76246d45b8c06..79bb750b5d1f4 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.cpp +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.cpp @@ -1,10 +1,10 @@ class Solution { public: long long flowerGame(int n, int m) { - long long count = (n + 1) / 2; - long long tol = (m + 1) / 2; - long long ecount = n / 2; - long long etol = m / 2; - return (count * etol + ecount * tol); + long long a1 = (n + 1) / 2; + long long b1 = (m + 1) / 2; + long long a2 = n / 2; + long long b2 = m / 2; + return a1 * b2 + a2 * b1; } -}; +}; \ No newline at end of file diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.go b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.go index 1fc18a0531c92..5d7ab96e6f761 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.go +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.go @@ -1,7 +1,5 @@ func flowerGame(n int, m int) int64 { - count := int64((n + 1) / 2) - tol := int64((m + 1) / 2) - ecount := int64(n / 2) - etol := int64(m / 2) - return count*etol + ecount*tol -} + a1, b1 := (n+1)/2, (m+1)/2 + a2, b2 := n/2, m/2 + return int64(a1*b2 + a2*b1) +} \ No newline at end of file diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.java b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.java index d6f5266efbcb6..eb89b015b1933 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.java +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.java @@ -1,9 +1,9 @@ class Solution { public long flowerGame(int n, int m) { - long count = (n + 1) / 2; - long tol = (m + 1) / 2; - long ecount = n / 2; - long etol = m / 2; - return (count * etol + ecount * tol); + long a1 = (n + 1) / 2; + long b1 = (m + 1) / 2; + long a2 = n / 2; + long b2 = m / 2; + return a1 * b2 + a2 * b1; } -} +} \ No newline at end of file diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.py b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.py index 5e4f8b5cd372e..1630f1d72d1f0 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.py +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.py @@ -1,7 +1,7 @@ class Solution: def flowerGame(self, n: int, m: int) -> int: - count = (n + 1) // 2 - tol = (m + 1) // 2 - ecount = n // 2 - etol = m // 2 - return count * etol + ecount * tol + a1 = (n + 1) // 2 + b1 = (m + 1) // 2 + a2 = n // 2 + b2 = m // 2 + return a1 * b2 + a2 * b1 diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.ts b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.ts new file mode 100644 index 0000000000000..0c7bec09af285 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.ts @@ -0,0 +1,5 @@ +function flowerGame(n: number, m: number): number { + const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1]; + const [a2, b2] = [n >> 1, m >> 1]; + return a1 * b2 + a2 * b1; +} diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.cpp b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.cpp new file mode 100644 index 0000000000000..1fb255acd895f --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + long long flowerGame(int n, int m) { + return ((long long) n * m) / 2; + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.go b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.go new file mode 100644 index 0000000000000..d2974c90960b4 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.go @@ -0,0 +1,3 @@ +func flowerGame(n int, m int) int64 { + return int64((n * m) / 2) +} \ No newline at end of file diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.java b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.java new file mode 100644 index 0000000000000..704a4f8fb8820 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.java @@ -0,0 +1,5 @@ +class Solution { + public long flowerGame(int n, int m) { + return ((long) n * m) / 2; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.py b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.py new file mode 100644 index 0000000000000..d33c2a8828268 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.py @@ -0,0 +1,3 @@ +class Solution: + def flowerGame(self, n: int, m: int) -> int: + return (n * m) // 2 diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.ts b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.ts new file mode 100644 index 0000000000000..e4d08d2364568 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.ts @@ -0,0 +1,3 @@ +function flowerGame(n: number, m: number): number { + return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n); +}