-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1922_Cnt_good_no.cpp
47 lines (37 loc) · 1.04 KB
/
1922_Cnt_good_no.cpp
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
37
38
39
40
41
42
43
44
45
46
47
/*
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).
Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7.
*/
//TC = O(log n) -> from myPow function SC = O(1)
#include <bits/stdc++.h>
using namespace std;
// Calculate the power of a number modulo MOD using binary exponentiation
int myPow(long long x, long long n, long long mod) {
int ans = 1;
while( n> 0) {
if(n % 2 == 0 ) {
x = (x * x) % mod;
n= n /2;
}
else {
ans = (ans * x) % mod;
n = n-1;
}
}
return ans;
}
int countGoodNumbers(long long n) {
long long mod = 1e9 + 7;
int ans;
long long even = n/2;
long long odd = n - even;
even = myPow(5, even, mod) ; // (5 possibilities: 0, 2, 4, 6, 8)
odd = myPow(4, odd, mod) ; // (4 possibilities: 2, 3, 5, 7)
ans = (even * odd) % mod;
return ans;
}
int main() {
long long n = 4;
cout << countGoodNumbers(n);
return 0;
}