分析
- 题目
- 思路
- 快速幂,注意处理溢出,模板题
代码
class Solution {
public:
using LL = long long;
const static int MOD = 1e9+7;
LL qmi(LL a, LL b){
LL res = 1;
while(b){
if(b&1) res = (res * a) % MOD;
b >>= 1;
a = a*a % MOD;
}
return res % MOD;
}
int countGoodNumbers(long long n) {
// 奇数位 偶数位 [(n+1)/2, 5] 奇数位 [n/2, 4]
LL even = (n+1)/2, odd = n/2;
return qmi(5, even)*(LL)qmi(4, odd) % MOD;
}
};