LCR 132. 砍竹子 II - 力扣(LeetCode)
可以打表或手写找找规律:
code:
typedef long long ll;
class Solution {
public:
const int mod = 1e9+7;
//快速幂
int power(int a,int n)
{
int ans=1;
while(n)
{
if(n&1) ans = ((ll)ans * a) % mod;
a = ((ll)a*a) % mod;
n>>=1;
}
return ans;
}
int cuttingBamboo(int n) {
if(n==2)return 1;
if(n==3)return 2;
int ans=0;
int t;
if(n%3 == 0)
{
t=n/3;
ans = power(3,t);
}
else if(n%3 == 1)
{
t=n/3-1;
ans = (ll)4*power(3,t)%mod;
}
else if(n%3 == 2)
{
t=n/3;
ans = (ll)2*power(3,t)%mod;
}
return ans;
}
};