class Solution {
public 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) ;
}
}
private long pow(int a, int b){
long ans = 1;
for(int i = 0; i < b; i ++){
ans = (ans * a) % p;
}
return ans ;
}
}
java
class Solution {
public int cuttingRope(int n) {
/*
分成等分的话乘积最大,1,2,3,4不可再拆分,拆分段长度为3是最优的,对3取模后余数为0或1或2,
1加到3里面形成4,2直接相乘
增加了大数取模的步骤,可用循环取余法和二分取余法
*/
if(n <= 2) {
return 1;
}
if(n == 3) {
return 2;
}
int res = n / 3;
int mod = n % 3;
int p = 1000000007;
if(mod == 0) {
res = (int)pow(3, res, p);
} else if(mod == 1) {
res = (int)(pow(3, res - 1, p) * 4 % p) ;
} else {
res = (int)(pow(3, res, p) * 2 % p);
}
return res;
}
//循环取余法,时间复杂度是o(n)
public long pow(int a, int x, int p) {
long result = 1;
for(int i = 1; i <= x; i++) {
result = (result * a) % p;
}
return result;
}
}
class Solution {
const int limit = 1000000007;
long pow(int a, int b) {
long res = 1;
for(int i= 0; i < b; i++)
res = (res * a) % limit;
return res;
}
public:
int cuttingRope(int n) {
if (n < 4) return n - 1;
int mod = n % 3;
if (mod == 1) return pow(3, n / 3 - 1) * 4 % limit;
else if (mod == 2) return pow(3, n / 3) * 2 % limit;
else return pow(3, n / 3) % limit;
}
};
class Solution {
public int cuttingRope(int n) {
if (n == 2) return 1;
if (n == 3) return 2;
if (n == 4) return 4;
long res = 1;
while (n > 4) {
n -= 3;
res = res * 3 % 1000000007;
}
res = res * n % 1000000007;
return (int) res;
}
}
// 剪绳子
// 时间复杂度 O(N)
// 空间复杂度 O(1)
class Solution {
private:
long pow(int a, int n) {
long res = 1;
int p = 1'000'000'007;
for (int i = 1; i <= n; ++i) {
res = res * a % p;
}
return res;
}
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 = 1'000'000'007;
if (mod == 0) { return pow(3, res) % p; }
else if (mod == 1) { return pow(3, res - 1) * 4 % p; }
else { return pow(3, res) * 2 % p; }
}
};
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 x, int n){
long sum = 1;
for(int i = 0; i < n; i++){
sum = (sum*x) % p;
}
return sum;
}
}
公式a^n % p = (a^(n-1)%p * a) %p
时间复杂度:O(n)
空间复杂度:O(1)
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 %p);
}
}
// 求a^n % p = (a^(n-1)%p * a) %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;
}
}
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 pow(3,res) % p;
}else if(mod == 1){
return (pow(3,res-1) *4) % p;
}else{
return (pow(3,res)*2)%p;
}
}
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;
}
};
// 时间O(n),空间O(1)
class Solution {
public:
int cuttingRope(int n) {
if(n<=3) return n-1;
int res = n/3;
int mod = n%3;
int tmp = 1e9+7;
long ans=1;
for(int i =1;i<res;++i)
{
ans = (ans*3)%tmp;
}
if(mod==0) return (int)(ans*3%tmp);
else if(mod==1) return (int)(ans*4%tmp);
else return (int)(ans*2*3%tmp); //*和%优先级相同
}
};
class Solution {
public int cuttingRope(int n) {
if(n<4) return n-1;
long res = 1L;
while(n>4){
res = (res*3)%1000000007;
n = n - 3;
}
return (int)((res * n)%1000000007);
}
}
//时间O(n) 空间O(n)
public int cuttingRope(int n) {
if(n == 2){
return 1;
}
if(n == 3){
return 2;
}
int count = n / 3; // 能分成几个3
int remainder = n % 3;
int result = 0;
if(remainder == 0){
result = (int)(pow(3,count) % 1000000007);
}
if(remainder == 1){
//凑成四
result = (int)(pow(3,count - 1) * 4 % 1000000007);
}
if(remainder == 2){
result = (int)(pow(3,count) * 2 % 1000000007);
}
return result ;
}
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;
}
//找规律
const int p = 1000000007;
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;
if (mod == 0) {
return pow(3, res) % p;
} else if (mod == 1) {
return (pow(3, res - 1) * 4) % p;
} else {
return (pow(3, res) * 2) % p;
}
}
long pow(int a, int n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (res * a) % p;
}
return res;
}
};
共以下 22 个回答
“`java
//数学是基础
class Solution {
public int cuttingBamboo(int bamboo_len) {
if(bamboo_len <= 3) { return bamboo_len - 1; } int remainder = bamboo_len % 3; int count3 = bamboo_len/3; if (remainder == 1) { count3--; } int p = 1000000007; long res = 1; while(count3 > 0) {
res = (res * 3)%p;
count3–;
}
if (remainder == 1) {
res = (res * 4)%p;
} else if (remainder == 2) {
res = (res * 2)%p;
}
return (int)res;
}
}
“`java
class Solution {
public int cuttingRope(int n) {
if (n <= 2) { return 1; } if (n == 3) { return 2; }
}
大树求余
“`C++ dp(n + 1, 0);
const int mod = 1e9 + 7;
int cuttingRope(int n) {
vector
dp[2] = 1; // 初始化,当绳子长度为 2 时只有一种剪法,剪成两段长度都为 1
}
“`
数学推到加上大数运算
模
时间复杂度是O(N), 空间复杂度是O(1)
【思路】总体思路与剪绳子1类似,需要加上mod处理:(ab)%x = ((a%x)(b%x))%x
【复杂度】时间复杂度O(n),空间复杂度O(1)
根据剪绳子1的结论,加上mod处理
时间:O(n)
空间:O(1)
MY帅弟
时间复杂度:O(n)
空间复杂度:O(1)
公式a^n % p = (a^(n-1)%p * a) %p
时间复杂度:O(n)
空间复杂度:O(1)
c++实现
int cuttingRope(int n) {
if(n<2)return 0;
if(n2) return 1;
if(n3) return 2;
int x = n/3;
int ll = n%3;
int mm=1000000007;
if(ll0)
{
return pow(3,x)%mm;
}
else if(ll1)
{
return pow(3,x-1)4%mm;
}
else return pow(3,x)2 %mm;
}
long pow(int x,int n)
{
long res = 1;
int mm = 1000000007;
for(int i = 0;i<n;i++)
{
res = (res*x)%mm;
}
return res;
}
时间复杂度:O(n)
空间复杂度:O(1)
思路:根据数学性质,将绳子每段剪成离e最近的整数3即可最大,通过迭代到只剩下>4的长度,最后一段直接乘以累积数值
时间复杂度:O(logn) 空间复杂度:O(1)
动态规划
复杂度O(n)