-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1922.cpp
More file actions
36 lines (27 loc) · 977 Bytes
/
1922.cpp
File metadata and controls
36 lines (27 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// If digits are in even position, choose from 5 options (even digits) : 0,2,4,6,8
// If digits are in odd position, choose from 4 options (Prime digits) : 2,3,5,7
// Total Good Numbers of length n: = 5^(n_even_positions) × 4^(n_odd_positions)
// TC : O(logn)
// SC : O(1)
class Solution {
public:
const int MOD = 1e9 + 7;
int countGoodNumbers(long long n) {
long long evenPosLength = (n + 1) / 2;
long long oddPosLength = n / 2;
long long evenWays = countPower(5, evenPosLength);
long long oddWays = countPower(4, oddPosLength);
return (evenWays * oddWays) % MOD;
}
long long countPower(long long base, long long power) {
long long result = 1;
base %= MOD;
while (power > 0) {
if (power % 2 == 1)
result = (result * base) % MOD;
base = (base * base) % MOD;
power /= 2;
}
return result;
}
};