解题思路:
由于数量级较大,需要使用long以及快速幂求余
下图便于理解快速幂求余
class Solution {
public int cuttingBamboo(int bamboo_len) {
if(bamboo_len <= 3) return bamboo_len - 1;
int b = bamboo_len % 3, p = 1000000007;
long rem = 1, x = 3;
for(int a = bamboo_len / 3 - 1; a > 0; a /= 2) {//a减一是为了少切分一个3,留给后面b的取值讨论
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 * 3 * 2 % p);
}
}