forked from Nanduag0/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Count_good_numbers.txt
41 lines (37 loc) · 924 Bytes
/
Count_good_numbers.txt
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
class Solution {
public:
int countGoodNumbers(long long n)
{
if(n==0)
return 0;
if(n==1)
return 5;
long long evencount,oddcount;
long long ans=1,ans1=1;
if(n&1)
{
evencount=((n/2)+1);
oddcount=(n/2);
}
else if(n%2==0)
{
evencount=((n)/2);
oddcount=((n/2));
}
ans=(power(5,(evencount))%1000000007);
ans1=(power(4,(oddcount))%1000000007);
return ((ans*ans1)%1000000007);
}
long long power(long long N,long long R)
{
if(R==0)
return 1;
long long res=power(N,(R/2))%1000000007;
long long val=((res%1000000007)*(res%1000000007))%1000000007;
if(R%2!=0)
{
val=(((val%1000000007)*(N%1000000007))%1000000007);
}
return val;
}
};