-
Notifications
You must be signed in to change notification settings - Fork 47
/
Third_Class.cpp
189 lines (152 loc) · 4.67 KB
/
Third_Class.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include<bits/stdc++.h>
using namespace std;
#define ll long long
// this function will test whether n is prime or not, time complexity - O(sqrt(n))
bool primeTest(int n) {
int sq = sqrt(n);
for(int i=2; i<=sq; i++) {
if(n % i == 0) return false;
}
return true;
}
/* Euclid's method for GCD of two numbers, time complexity - O(log(min(a, b))) */
// Think out worst case input for Euclid's algorithm
int GCD(int a, int b) {
if(b == 0) return a;
return GCD(b, a % b);
}
// time complexity - O(n)
ll power(ll a, ll b) {
if(b == 0) return 1LL;
if(b % 2 == 0) return power(a, b/2) * power(a, b/2);
return a*power(a, b/2)*power(a, b/2);
}
// recursive implementation of modular exponentiation, O(logn)
ll power(ll a, ll b, ll mod) {
if(b == 0) return 1LL;
ll ans = power(a, b/2);
ans *= ans;
ans %= mod;
if(b % 2 != 0) {
ans *= a;
ans %= mod;
}
return ans;
}
/* Modular exponentiation, time complexity - O(logn), iterative implementation */
ll binpow(ll a, ll b, ll mod) {
ll ans = 1;
a %= mod;
while(b) {
if(b % 2) {
ans *= a;
ans %= mod;
}
a = a * a % mod;
b >>= 1;
}
return ans %= mod;
}
int N = 1000;
signed main() {
/* Q. Given an array of n elements and all elements are initially zero.
Process q queries where each query will ask you to add 1 from index l to r,
After processing all such queries you have to print the entire array.
1 <= n <= 10^5
1 <= q <= 10^5
*/
/*sol . For each query add +1 at index l and -1 at index r+1,
after processing all such queries just take prefix sum of whole array
*/
int q;
cin >> q;
int arr[N+5];
memset(arr, 0, sizeof arr);
for(int i=1; i<=q; i++) {
int l, r;
cin >> l >> r;
arr[l] += 1;
arr[r+1] -= 1;
}
for(int i=1; i<=N; i++) arr[i] += arr[i-1]; // our array arr is 1-indexed.
/*Q1. Count the number of pairs whose sum is equal to given k.
Const:-
1 <= n <= 100000
1 <= k <= 100000 */
int n, k;
cin >> n >> k;
ll ans = 0;
int freq[100005];
memset(freq, 0, sizeof freq);
for(int i=0; i < n; i++){
freq[arr[i]]++;
}
for(int i=0; i <= k; i++){
// for i==k-i
// Suppose, k=2 and arr={1,1,1} freq[1] is 3 and freq[2-1] is also three
// So we need to add freq[i]*(freq[k-i]-1) only
if(i == k-i)
ans += (ll)freq[i]*(freq[k-i]-1);
else
ans += (ll)freq[i]*freq[k-i];
}
ans = ans/2;
cout << ans << '\n';
// prime number
n = 31;
cout << primeTest(n) << '\n';
// Calculate all divisors of a number, time complexity - O(sqrt(n))
int num = 50;
vector<int> divs;
int sq1 = sqrt(num);
for(int i=1; i<=sq1; i++) {
if(num % i == 0) {
divs.push_back(i);
if(num/i != i) {
divs.push_back(num/i); // divs will contain all divisors of num.
}
}
}
for(int x : divs) cout << x << ' ';
// GCD(greatest common divisor), time complexity - O(min(a, b))
int a1 = 15, b1 = 20;
for(int i=min(a1, b1); i>=1; i--) {
if(a1 % i == 0 && b1 % i == 0) {
cout << "Gcd of " << a1 << " and " << b1 << ": " << i << '\n';
break;
}
}
// above method of calculating GCD of two numbers is not efficient
/*Efficient method of calculating GCD */
cout << GCD(a1, b1) << '\n';
cout << __gcd(a1, b1) << '\n'; // internally implemented in C++
/*Modular properties
1. (a + b)%m = (a%m + b%m)%m
2. (a - b)%m = (a%m - b%m + m)%m
3. (a*b)%m = ((a%m)*(b%m))%m
4. (a/b)%m = ((a%m)*(b^-1%m))%m ---- b^-1 is modular inverse of b under modulo m
*/
/* Fermat's little theorem
If p is a prime number, then (a^p)-a is multiple of p
Now add one more constraint to above theorem
Constraint -> If a is not divisble by p
then (a^(p-1))-1 is multiple of p.
modular inverse of a under modulo p = (a^(p-2)) % p;
How fast can you compute a^-1 ? // worst case O(p) by single loop
*/
/*Slow method for finding a^-1 */
ll inv = 1, nn = 2, p = 5;
for(long long i=1; i<=p-2; i++) {
inv *= nn;
inv %= p;
}
cout << inv << "\n";
/*We need some faster method to calculate (a^-1)%p */
/* Modular exponentiation, time complexity - O(logn) */
ll aa = 3, bb = 4, mod = 4;
cout << power(aa, bb, mod) << '\n';
aa = 4, mod = 1e9 + 7;
// modular inverse
cout << power(aa, mod-2, mod) << '\n';
return 0;
}