class Solution {
public int cuttingRope(int n) {
if(n <= 2) return 1;
if(n == 3) return 2;
int res = n / 3;
int mod = n % 3;
int p = 1000000007;
if(mod == 0){
return (int)pow(3, res) % p;
} else if(mod == 1){
return (int)(pow(3, res - 1) * 4 % p);
} else {
return (int)(pow(3, res) * 2 % p);
}
}
public long pow(int a, int n){
long res = 1;
int p = 1000000007;
for(int i= 1; i <= n; i++){
res = (res * a) % p;
}
return res;
}
}
def cuttingRope(n):
# 2、3为特殊情况
if n == 2:
return 1
if n == 3:
return 2
res = n // 3
mod = n % 3
p = 1000000007
if mod == 0:
return pow(3,res)
if mod == 1:
# 如果余数是1,将1和一个3凑成一个4
return pow(3,res-1) * 4 % p
if mod == 2:
return pow(3,res) * 2 % p
# 虽然python不会出现大数越界,但是还是给出方法
def pow(x,y):
res = 1
for _ in range(y):
res = (x * res) % 1000000007
return res
class Solution {
public int cuttingRope(int n) {
if (n <= 3) return n -1;
long ans = 1;
while (n > 4) {
ans = (ans * 3) % 1000000007;
n -= 3;
}
return (int) (ans * n % 1000000007);
}
}
func cuttingRope(n int) int {
if n < 4 {
return n - 1
}
var p int = 1000000007
res := 1
//循环取余
for n > 4 {
res *= 3
res %= p
n -= 3
}
return res * n % p
}
class Solution:
def cuttingRope(self, n: int) -> int:
# 推理判断:当绳子长度大于3时,需要尽量把绳子切分成差不多一样的长度,且不能同时存在2和4的长度,2+4=3+3
# 特殊情况
if n == 2:
return 1
if n == 3:
return 2
# 绳子长度除3取整表示可以切分成这么多段的3,余数为1时可以合并一个3,余数为2时保持不变
res = n // 3
mod, p = n % 3, 1000000007
if mod == 0:
return 3 ** res % p
elif mod == 1:
return 3 ** (res - 1) * 4 % p
else:
return 3 ** res * 2 % p
class Solution {
public int cuttingRope(int n) {
if(n <= 3){
return n - 1;
}
if(3 < n){
int count = n / 3;
int mode = n % 3;
int p = 1000000007;
if(mode == 2){
return (int)(pow(3, count)* 2 % p);
}
if(mode == 1){
return (int)(pow(3, count-1) * 4 % p);
}
return (int)pow(3,count);
}
return -1;
}
//取余
long pow(int a, int n){
long res = 1;
int p = 1000000007;
for(int i = 1; i <= n; i++){
res = (res * a) % p;
}
return res;
}
}
class Solution {
public int cuttingRope(int n) {
if (n <= 2) {
return 1;
}
if (n == 3) {
return 2;
}
int res = n / 3;
int mod = n % 3;
int p = 1000000007;
if (mod == 0) {
return (int) pow(3, res);
} else if (mod == 1) {
return (int) (pow(3, res - 1) * 4 % p);
} else {
return (int) (pow(3, res) * 2 % 1000000007);
}
}
//取模运算
long pow(int a, int n) {
long res = 1;
int p = 1000000007;
for (int i = 1; i <= n; i++) {
res = (res * a) % p;
}
return res;
}
}
/**
* 剑指 Offer 14- II. 剪绳子 II
*
* @param n
* @return
*/
public int cuttingRope2(int n) {
//时间复杂度O(logn)
//空间复杂度O(1)
if (n <= 3) return n - 1;
int b = n % 3, p = 1000000007;
long rem = 1, x = 3;
for (int a = n / 3 - 1; a > 0; a /= 2) {
if (a % 2 == 1) {
rem = (rem * x) % p;
}
x = (x * x) % p;
}
if (b == 0) return (int) (rem * 3 % p);
if (b == 1) return (int) (rem * 4 % p);
return (int) (rem * 6 % p);
}
/**
快速幂求余,太妙了
如果是奇数需要再乘一次x
时间O(logn)
空间O(1)
*/
class Solution {
public int cuttingRope(int n) {
if(n<=3) return n-1;
long res = 1, x = 3;
int b = n % 3, p = 1000000007;
for(int i=n/3-1; i>0; i/=2){
if(i%2==1){
res = res * x % p;
}
x = x * x % p;
}
if(b==0) return (int) (res * 3 % p);
if(b==1) return (int) (res * 4 % p);
return (int) (res * 6 % p);
}
}
class Solution:
def cuttingRope(self, n: int) -> int:
if n<4: return n-1
k,m,mod = n//3,n%3,1000000007
if m==0: return (3**k)%mod
if m==1: return (3**(k-1)*4)%mod
else: return (3**k*2)%mod
class Solution:
def cuttingRope(self, n: int) -> int:
if n<=3: return n-1
sum = 1
while n>4:
sum *=3
n -=3
return (sum*n)%1000000007
class Solution {
public int cuttingRope(int n) {
int mod = (int)1e9 + 7;
if(n <= 3) return n - 1;
int r = n % 3;
int l = n / 3;
if(r == 0){
return (int)(pow(3,l));//全是3 都变成3
}else if(r == 1){
//剩下3+1 乘以4
return (int)(pow(3,l-1) * 4 % mod);
}else if(r == 2){
return (int)(pow(3,l) * 2 % mod);//剩余2
}
return 0;
}
public long pow(int a,int n){
long res = 1;
int mod = (int)1e9 + 7;
while(n != 0){
res *= a;
res %= mod;
n--;
}
return res;
}
}
class Solution {
public int cuttingRope(int n) {
if(n<=2) return 1;
if(n==3) return 2;
int res = n/3;
int mod = n%3;
int p = 1000000007;
if(mod == 0){
return (int)(pow(3,res) % p);
}else if(mod == 1){
return (int)(pow(3,res-1) * 4 % p);
}else{
return (int)(pow(3,res) * 2 % p);
}
}
public long pow(int n,int res){
long sum = 1;
int p = 1000000007;
for(int i = 1;i<=res;i++){
sum = (sum * n) %p;
}
return sum;
}
}
class Solution {
int p = 1000000007;
public int cuttingRope(int n) {
if (n <= 2) {
return 1;
}
if (n == 3) {
return 2;
}
int res = n / 3;
int mod = n % 3;
if (mod == 0) {
return (int)pow(3, res);
} else if (mod == 1) {
return (int)(pow(3, res - 1) * 4 % p);
} else {
return (int)(pow(3, res) * 2 % p);
}
}
public long pow(int a, int n) {
long res = 1;
for (int i = 0; i < n; i++) {
res = (res * a) % p;
}
return res;
}
}
共以下 21 个回答
取余防止结果数值过大
贪心写法
时间O(n) 空间O(1)
python无大数越界问题,理论上 Python 中的变量取值范围由系统内存大小决定
时间:O(1)
空间:O(1)
这里不能用动态规划,因为数值会溢出,如果对中间值取余的化,中间就无法比较数值大小,只能用数学知识。用快速幂取余,时间复杂度O(logN),空间复杂度O(1)。
循环求余
快速幂取模时间复杂度为O(lgn),空间复杂度为O(1).
和上一个题差不多,结果取mod
大数取模
时间复杂度O(N)
通过数学知识可以直接化简得到答案,基本高数知识就可以。
时间/空间 O(1)
时间O(n)
空间O(1)
时间:O(1),空间:O(1)