class Solution {
public:
int cuttingRope(int n) {
if(n <= 3) return n-1;
long long res = 1;
int p = 1000000007;
if(n % 3 == 1) res = 4, n -= 4;
else if(n % 3 == 2) res = 2, n -= 2;
while(n){
res = (res * 3) % p;
n -= 3;
}
return res;
}
};
“`c++
class Solution {
public:
int cuttingRope(int n) {
if (n == 2) return 1;
if (n == 3) return 2;
long sum = 1;
while (n > 4) {
sum *= 3;
sum=sum%1000000007;//取模
n -= 3;
}
return (int)(sum * n % 1000000007);
}
};
c++ 代码 和上一题类似
class Solution {
public:
int cuttingRope(int n)
{
long long s=1;
if(n1||n2) return 1;
if(n3) return 2;
while(n>4)
s*=3,n-=3,s%=1000000007;
class Solution {
public:
int cuttingRope(int n) {
int mod = 1e9+7;
if(n <= 3){
return n - 1;
}
int res = n/3;
int remain = n%3;
if(remain == 0){
return (pow(3, res))%mod;
}
else if(remain == 1){
return (pow(3, res - 1) * 4)%mod;
}
else{
return (pow(3, res) * 2)%mod;
}
}
//这里用std里的pow会超出int的范围,所以需要自己定义一个long类型的pow函数
long pow(int a, int n){
long ans = 1;
int mod = 1e9+7;
while(n--){
ans = ans*a%mod;
}
return ans;
}
};
class Solution {
public int cuttingRope(int n) {
if(n<=3) return n-1;
int x = n/3,y=n%3;
if(y==0) return (int) (pow(3,x)%1000000007);
else if(y==1) return (int) (pow(3,x-1)*4%1000000007);
else return (int) (pow(3,x)*2%1000000007);
}
public long pow(int a,int n) {
long sum = 1;
int p = 1000000007;
for(int i=1;i<=n;i++) {
sum = sum*a%p;
}
return sum;
}
}
class Solution {
public:
long long cuttingRope(int n) {
int res = n / 3;
int mod = n % 3;
if(n <= 3){
return n - 1;
}
int p = 1000000007;
long long sum = 0;
if(mod == 0){
sum = pow(3,res) % p;
}
else if(mod == 1){
sum = (pow(3,res - 1) * 4) % p;
}
else{
sum = (pow(3,res) * 2) % p;
}
return sum;
}
long long pow(int a,int n){
long long sum = 1;
int p = 1000000007;
for(int i = 0;i < n;++i){
sum = (sum * a) % p;
}
return sum;
}
};
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);
}
}
//求 a^n %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<=2) return 1;
else if(n==3) return 2;
else{
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%1000000007);
}else{
return (int)(pow(3,res)*2%1000000007);
}
}
}
long pow(int a, int n){
long res = 1;
int p = 1000000007;
for(int i=0;i<n;i++){
res = (res*a)%p;
}
return res;
}
}
//贪心算法
class Solution {
public int cuttingRope(int n) {
if(n <= 3) return n - 1;
long res = 1;
while(n > 4){
res = res * 3 % 1000000007;
n -= 3;
}
return (int)(res * n % 1000000007);
}
}
class Solution {
public int cuttingRope(int n) {
if(n <= 3) return n - 1;
int a = n % 3, b = n / 3;
int p = 1000000007;
if(a == 0) {
return (int)(pow(3,b) % p);
}
if(a == 1) {
return (int)(pow(3,b-1) * 4 % p);
}
return (int)(pow(3,b) * 2 % p);
}
public long pow(int a, int b) {
long res = 1;
int p = 1000000007;
for(int i = 1; i <= b; i ++) {
res = (res * a) % p;
}
return res;
}
}
共以下 15 个回答
“`c++
class Solution {
public:
int cuttingRope(int n) {
if (n == 2) return 1;
if (n == 3) return 2;
long sum = 1;
while (n > 4) {
sum *= 3;
sum=sum%1000000007;//取模
n -= 3;
}
return (int)(sum * n % 1000000007);
}
};
c++ 代码 和上一题类似
class Solution {
public:
int cuttingRope(int n)
{
long long s=1;
if(n1||n2) return 1;
if(n3) return 2;
while(n>4)
s*=3,n-=3,s%=1000000007;
};
”’java
class Solution {
public int cuttingRope(int n) {
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);
}
”’
时间复杂度O(logn)
空间复杂度O(1)
大数求幂,需要注意一下
大数求幂, 以前没有留意过这类问题
时间复杂度:本题的时间主要花在 pow 函数上,采用快速幂可以 logn 的时间复杂度
空间:O(1)
取余
7.26
Day18:下面是代码
代码
以下是代码
余数部分的知识还有所欠缺,看视频补了一下