Skip to content

Commit 6112294

Browse files
authoredSep 14, 2023
feat: add solutions to lc/lcof problems (doocs#1625)
quick pow
1 parent fef92e0 commit 6112294

File tree

63 files changed

+1659
-1607
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1659
-1607
lines changed
 

‎lcof/面试题14- II. 剪绳子 II/README.md

+72-70
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [面试题 14- II. 剪绳子 II](这里是题目链接,如:https://leetcode.cn/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/)
1+
# [面试题 14- II. 剪绳子 II](https://leetcode.cn/problems/jian-sheng-zi-ii-lcof/)
22

33
## 题目描述
44

@@ -36,7 +36,7 @@
3636

3737
当 $n \lt 4$,此时 $n$ 不能拆分成至少两个正整数的和,因此 $n - 1$ 是最大乘积。当 $n \ge 4$ 时,我们尽可能多地拆分 $3$,当剩下的最后一段为 $4$ 时,我们将其拆分为 $2 + 2$,这样乘积最大。
3838

39-
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
39+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。
4040

4141
<!-- tabs:start -->
4242

@@ -59,30 +59,30 @@ class Solution:
5959

6060
```java
6161
class Solution {
62+
private final int mod = (int) 1e9 + 7;
63+
6264
public int cuttingRope(int n) {
6365
if (n < 4) {
6466
return n - 1;
6567
}
66-
final int mod = (int) 1e9 + 7;
6768
if (n % 3 == 0) {
68-
return (int) qmi(3, n / 3, mod);
69+
return qpow(3, n / 3);
6970
}
7071
if (n % 3 == 1) {
71-
return (int) (qmi(3, n / 3 - 1, mod) * 4 % mod);
72+
return (int) (4L * qpow(3, n / 3 - 1) % mod);
7273
}
73-
return (int) (qmi(3, n / 3, mod) * 2 % mod);
74+
return 2 * qpow(3, n / 3) % mod;
7475
}
7576

76-
long qmi(long a, long k, long p) {
77-
long res = 1;
78-
while (k != 0) {
79-
if ((k & 1) == 1) {
80-
res = res * a % p;
77+
private int qpow(long a, long n) {
78+
long ans = 1;
79+
for (; n > 0; n >>= 1) {
80+
if ((n & 1) == 1) {
81+
ans = ans * a % mod;
8182
}
82-
k >>= 1;
83-
a = a * a % p;
83+
a = a * a % mod;
8484
}
85-
return res;
85+
return (int) ans;
8686
}
8787
}
8888
```
@@ -97,53 +97,24 @@ public:
9797
return n - 1;
9898
}
9999
const int mod = 1e9 + 7;
100+
auto qpow = [&](long long a, long long n) {
101+
long long ans = 1;
102+
for (; n; n >>= 1) {
103+
if (n & 1) {
104+
ans = ans * a % mod;
105+
}
106+
a = a * a % mod;
107+
}
108+
return (int) ans;
109+
};
100110
if (n % 3 == 0) {
101-
return qmi(3, n / 3, mod);
111+
return qpow(3, n / 3);
102112
}
103113
if (n % 3 == 1) {
104-
return qmi(3, n / 3 - 1, mod) * 4 % mod;
105-
}
106-
return qmi(3, n / 3, mod) * 2 % mod;
107-
}
108-
109-
long qmi(long a, long k, long p) {
110-
long res = 1;
111-
while (k != 0) {
112-
if ((k & 1) == 1) {
113-
res = res * a % p;
114-
}
115-
k >>= 1;
116-
a = a * a % p;
117-
}
118-
return res;
119-
}
120-
};
121-
```
122-
123-
### **JavaScript**
124-
125-
```js
126-
/**
127-
* @param {number} n
128-
* @return {number}
129-
*/
130-
var cuttingRope = function (n) {
131-
if (n <= 3) return n - 1;
132-
let a = ~~(n / 3);
133-
let b = n % 3;
134-
const MOD = 1e9 + 7;
135-
function myPow(x) {
136-
let r = 1;
137-
for (let i = 0; i < x; i++) {
138-
r = (r * 3) % MOD;
114+
return qpow(3, n / 3 - 1) * 4L % mod;
139115
}
140-
return r;
141-
}
142-
if (b === 1) {
143-
return (myPow(a - 1) * 4) % MOD;
116+
return qpow(3, n / 3) * 2 % mod;
144117
}
145-
if (b === 0) return myPow(a) % MOD;
146-
return (myPow(a) * 2) % MOD;
147118
};
148119
```
149120
@@ -155,26 +126,57 @@ func cuttingRope(n int) int {
155126
return n - 1
156127
}
157128
const mod = 1e9 + 7
129+
qpow := func(a, n int) int {
130+
ans := 1
131+
for ; n > 0; n >>= 1 {
132+
if n&1 == 1 {
133+
ans = ans * a % mod
134+
}
135+
a = a * a % mod
136+
}
137+
return ans
138+
}
158139
if n%3 == 0 {
159-
return qmi(3, n/3, mod)
140+
return qpow(3, n/3)
160141
}
161142
if n%3 == 1 {
162-
return qmi(3, n/3-1, mod) * 4 % mod
143+
return qpow(3, n/3-1) * 4 % mod
163144
}
164-
return qmi(3, n/3, mod) * 2 % mod
145+
return qpow(3, n/3) * 2 % mod
165146
}
147+
```
166148

167-
func qmi(a, k, p int) int {
168-
res := 1
169-
for k != 0 {
170-
if k&1 == 1 {
171-
res = res * a % p
172-
}
173-
k >>= 1
174-
a = a * a % p
175-
}
176-
return res
177-
}
149+
### **JavaScript**
150+
151+
```js
152+
/**
153+
* @param {number} n
154+
* @return {number}
155+
*/
156+
var cuttingRope = function (n) {
157+
if (n < 4) {
158+
return n - 1;
159+
}
160+
const mod = 1e9 + 7;
161+
const qpow = (a, n) => {
162+
let ans = 1;
163+
for (; n; n >>= 1) {
164+
if (n & 1) {
165+
ans = Number((BigInt(ans) * BigInt(a)) % BigInt(mod));
166+
}
167+
a = Number((BigInt(a) * BigInt(a)) % BigInt(mod));
168+
}
169+
return ans;
170+
};
171+
const k = Math.floor(n / 3);
172+
if (n % 3 === 0) {
173+
return qpow(3, k);
174+
}
175+
if (n % 3 === 1) {
176+
return (4 * qpow(3, k - 1)) % mod;
177+
}
178+
return (2 * qpow(3, k)) % mod;
179+
};
178180
```
179181

180182
### **Rust**
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
class Solution {
2-
public:
3-
int cuttingRope(int n) {
4-
if (n < 4) {
5-
return n - 1;
6-
}
7-
const int mod = 1e9 + 7;
8-
if (n % 3 == 0) {
9-
return qmi(3, n / 3, mod);
10-
}
11-
if (n % 3 == 1) {
12-
return qmi(3, n / 3 - 1, mod) * 4 % mod;
13-
}
14-
return qmi(3, n / 3, mod) * 2 % mod;
15-
}
16-
17-
long qmi(long a, long k, long p) {
18-
long res = 1;
19-
while (k != 0) {
20-
if ((k & 1) == 1) {
21-
res = res * a % p;
22-
}
23-
k >>= 1;
24-
a = a * a % p;
25-
}
26-
return res;
27-
}
1+
class Solution {
2+
public:
3+
int cuttingRope(int n) {
4+
if (n < 4) {
5+
return n - 1;
6+
}
7+
const int mod = 1e9 + 7;
8+
auto qpow = [&](long long a, long long n) {
9+
long long ans = 1;
10+
for (; n; n >>= 1) {
11+
if (n & 1) {
12+
ans = ans * a % mod;
13+
}
14+
a = a * a % mod;
15+
}
16+
return (int) ans;
17+
};
18+
if (n % 3 == 0) {
19+
return qpow(3, n / 3);
20+
}
21+
if (n % 3 == 1) {
22+
return qpow(3, n / 3 - 1) * 4L % mod;
23+
}
24+
return qpow(3, n / 3) * 2 % mod;
25+
}
2826
};

0 commit comments

Comments
 (0)
Please sign in to comment.