File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countGoodNumbers (self , n : int ) -> int :
3
+ mod_val = 10 ** 9 + 7
4
+ # evens
5
+ evens = 1
6
+ tot_cases = (n // 2 ) + (n % 2 )
7
+ prev = []
8
+ if tot_cases > 0 :
9
+ evens = 5
10
+ x = 1
11
+ while 2 * x <= tot_cases :
12
+ prev .append ((x , evens ))
13
+ evens = (evens * evens ) % mod_val
14
+ x *= 2
15
+ while prev :
16
+ i , pow5 = prev .pop ()
17
+ if x + i <= tot_cases :
18
+ x += i
19
+ evens *= pow5
20
+
21
+ # odds
22
+ odds = 1
23
+ tot_cases = (n // 2 )
24
+ prev = []
25
+ if tot_cases > 0 :
26
+ odds = 4
27
+ x = 1
28
+ while 2 * x <= tot_cases :
29
+ prev .append ((x , odds ))
30
+ odds = (odds * odds ) % mod_val
31
+ x *= 2
32
+ while prev :
33
+ i , pow4 = prev .pop ()
34
+ if x + i <= tot_cases :
35
+ x += i
36
+ odds *= pow4
37
+
38
+
39
+ return (evens * odds ) % (mod_val )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countGoodNumbers (self , n : int ) -> int :
3
+ def _helper (tot_cases : int , poww : int , mod_val : int = 10 ** 9 + 7 ) :
4
+ if tot_cases == 0 :
5
+ return 1
6
+ output , x = poww , 1
7
+ prev = []
8
+ while 2 * x <= tot_cases :
9
+ prev .append ((x , output ))
10
+ output = (output * output ) % mod_val
11
+ x *= 2
12
+ while prev :
13
+ i , poww = prev .pop ()
14
+ if x + i <= tot_cases :
15
+ x += i
16
+ output = (output * poww ) % mod_val
17
+ return output
18
+
19
+ mod_val = 10 ** 9 + 7
20
+ return (
21
+ _helper ((n // 2 ) + (n % 2 ), 5 , mod_val ) *
22
+ _helper ((n // 2 ), 4 , mod_val )
23
+ ) % (mod_val )
You can’t perform that action at this time.
0 commit comments