leetcode/solution/1900-1999/1922.Count Good Numbers/Solution.java

19 lines
437 B
Java

class Solution {
private final int mod = (int) 1e9 + 7;
public int countGoodNumbers(long n) {
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
}
private long qpow(long x, long n) {
long res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return res;
}
}