|
| 1 | +--- |
| 2 | +id: alternating-digit-sum |
| 3 | +title: Count Collisions of Monkeys on a Polygonaximum |
| 4 | +sidebar_label: 2550 Count Collisions of Monkeys on a Polygonaximum |
| 5 | + - Array |
| 6 | + - Recursion |
| 7 | + - LeetCode |
| 8 | + - C++ |
| 9 | +description: "This is a solution to the Count Collisions of Monkeys on a Polygonaximum problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +There is a regular convex polygon with n vertices. The vertices are labeled from 0 to n - 1 in a clockwise direction, and each vertex has exactly one monkey. The following figure shows a convex polygon of 6 vertices. |
| 15 | + |
| 16 | +Simultaneously, each monkey moves to a neighboring vertex. A collision happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge. |
| 17 | + |
| 18 | +Return the number of ways the monkeys can move so that at least one collision happens. Since the answer may be very large, return it modulo 10^9 + 7. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +Input: n = 3 |
| 26 | +Output: 6 |
| 27 | +Explanation: |
| 28 | +There are 8 total possible movements. |
| 29 | +
|
| 30 | +``` |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: n = 4 |
| 36 | +Output: 14 |
| 37 | +
|
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | +### Constraints |
| 42 | + |
| 43 | +- `3 <= n <= 10^9` |
| 44 | + |
| 45 | + |
| 46 | +### Approach |
| 47 | + |
| 48 | +According to the problem description, each monkey has two ways of moving, either clockwise or counterclockwise. Therefore, there are a total of $2^n$ ways to move. The non-collision ways of moving are only two, that is, all monkeys move clockwise or all monkeys move counterclockwise. Therefore, the number of collision ways of moving is $2^n - 2$. |
| 49 | + |
| 50 | +We can use fast power to calculate the value of $2^n$, then use $2^n - 2$ to calculate the number of collision ways of moving, and finally take the remainder of $10^9 + 7$. |
| 51 | + |
| 52 | +The time complexity is $O(\log n)$, where $n$ is the number of monkeys. The space complexity is $O(1)$. |
| 53 | + |
| 54 | +#### Python3 |
| 55 | + |
| 56 | +```python |
| 57 | +class Solution: |
| 58 | + def monkeyMove(self, n: int) -> int: |
| 59 | + mod = 10**9 + 7 |
| 60 | + return (pow(2, n, mod) - 2) % mod |
| 61 | +``` |
| 62 | + |
| 63 | +#### Java |
| 64 | + |
| 65 | +```java |
| 66 | +class Solution { |
| 67 | + public int monkeyMove(int n) { |
| 68 | + final int mod = (int) 1e9 + 7; |
| 69 | + return (qpow(2, n, mod) - 2 + mod) % mod; |
| 70 | + } |
| 71 | + |
| 72 | + private int qpow(long a, int n, int mod) { |
| 73 | + long ans = 1; |
| 74 | + for (; n > 0; n >>= 1) { |
| 75 | + if ((n & 1) == 1) { |
| 76 | + ans = ans * a % mod; |
| 77 | + } |
| 78 | + a = a * a % mod; |
| 79 | + } |
| 80 | + return (int) ans; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### C++ |
| 86 | + |
| 87 | +```cpp |
| 88 | +class Solution { |
| 89 | +public: |
| 90 | + int monkeyMove(int n) { |
| 91 | + const int mod = 1e9 + 7; |
| 92 | + using ll = long long; |
| 93 | + auto qpow = [&](ll a, int n) { |
| 94 | + ll ans = 1; |
| 95 | + for (; n; n >>= 1) { |
| 96 | + if (n & 1) { |
| 97 | + ans = ans * a % mod; |
| 98 | + } |
| 99 | + a = a * a % mod; |
| 100 | + } |
| 101 | + return ans; |
| 102 | + }; |
| 103 | + return (qpow(2, n) - 2 + mod) % mod; |
| 104 | + } |
| 105 | +}; |
| 106 | +``` |
0 commit comments