-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path56.cpp
More file actions
175 lines (104 loc) · 3.37 KB
/
Copy path56.cpp
File metadata and controls
175 lines (104 loc) · 3.37 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <bits/stdc++.h>
using namespace std;
// Large Exponentiation with ETF & Euler's Theorem
const int M = 1e9+7;
// if b <= 10^18
/*
long long b = 10^18
then log(10^18) bits ~ 18 * log(10) ~ 59
and there are 64 bits in long long thus this code will work
*/
// int binExp(int a, long long b){
// int ans = 1;
// while (b)
// {
// if(b&1){
// ans = (ans * 1LL * a) % M;
// }
// a = (a * 1LL * a) % M;
// b>>=1;
// }
// return ans;
// }
/*
we can't give value of b bigger than 10^18, we will indirectly give this
*consider, we need to calculate this and given M = 10^9+7
32
64^
50^
here it is of form like a^(b^c)
here b -> b^c
* a and b are coprime numbers if the common factor between them is only 1
i.e. gcd(a, b) = 1
* ETF -> Euler Totient Function
ETF value of N is count of k
such that 1 <= k <= N and
k, N are coprime
5 -> 1, 2, 3, 4 = 4 (5, 5 are not coprime because they have two common factors 1 and 5, also gcd(5, 5)= 5)
ETF is represented through phi
Φ(5) = 4
Φ(6) = 2 {1, 5}
__
Φ(n) = n * | | (1 - (1/p))
where p is all the distinct prime factors of n ( 1 is neither prime nor composite)
Φ(5) = 5 * (1 - 1/5) = 4
Φ(6) = 6 * (1 - 1/2) * (1 - 1/3) = 2
* Euler Theorem
b*modΦ(n)
a^b ≅ a^ mod(n) ≅ -> is called congruence
a ≅ b mod(n) -> it means if we divide a with n then b will come as remainder
that means b*modΦ(n)
if we divide a^b with n then a^ will come as remainder
( b % Φ(n) )
a^b % n = ( a^ ) % n
Thus conclusion of Euler Theorem
( b % Φ(M) )
a^b % M = ( a^ ) % M where M is any number
if n is prime ( and in most cases M is prime)
then
Φ(n) = n(1 - (1/n))
= n - 1
thus ETF value of prime no. is n-1
Euler Theorem when M is prime
( b % (M - 1) )
a^b % M = ( a^ ) % M where M is prime number
*/
int binExp(int a, long long b, int m){
int ans = 1;
while (b)
{
if(b&1){
ans = (ans * 1LL * a) % m;
}
a = (a * 1LL * a) % m;
b>>=1;
}
return ans;
}
int main() {
// 50^64^32 % M
// since M is prime 64^32 will have to do % with M-1
// cout<<binExp(50, binExp(64, 32, M-1), M);
// this is how we can handle very big values of b
// all these above theory was to find out how we can integrate % M with the power so that we can make it small
// and with the help of euler's theorem we have find it out
//Now let's do a question
// https://leetcode.com/problems/super-pow/description/
/*
1337 is not a prime no.
1337 = 7 * 191
Φ(1337) = 1337*(1 - 1/7)*(1 - 1/191 ) = 6 * 190 = 1140
b is given in form of an array
b
a^ % 1337
b%1140
= a^ % 1337
now let's say b is [4, 3, 3, 8, 5, 2]
then we can write complete no. as ( 4*10^5 + 3*10^4 + 3*10^3 + 8*10^2 + 5*10^1 + 2*10^0 ) % 1440
and calculate modulo of each
*/
/*
Φ(1440) = 1440*(1 - 1/2)*(1 - 1/5)*(1 - 1/9) = 512
*/
return 0;
}