avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

6338. Number of methods of monkey collision Weekly

  2024-01-01        
字数统计: 148字   |   阅读时长: 1min

2023-01-29 (1).png
6338. Number of methods of monkey collision

Thought:

纯math问题,that is$2^n - 2$。butnAt most$10^9$So big,Return directly to timeout。
所以用了Fast power算法,

使用Fast power算法来降低计算 2^n Time complexity。
it’s here,We use variables x To store 2^n,And in each cycle x square。when n When you are a miracle,We will take the result to take the result x。This can be calculated in each cycle2^n。
Before returning the result,We need to use(res-2)%mod Remove the border situation

Code:

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def monkeyMove(self, n: int) -> int:
mod = 10**9 + 7
res = 1
x = 2
while n > 0:
if n % 2 == 1:
res = (res * x) % mod
x = (x * x) % mod
n = n // 2
return (res-2) % mod
  • Python
  • answer
  • math
  • Fast power
  • Weekly

扫一扫,分享到微信

微信分享二维码
6324. Maximize the great value of the array
6331The most prizes won in the two lines make()usage
目录
  1. 1. Thought:
  2. 2. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号