Skip to content

Commit ab88eff

Browse files
Added some concepts and problems
1 parent 6548240 commit ab88eff

8 files changed

+389
-0
lines changed

Number Theory/(ATCODER)177E.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
int mod = 1e9 + 7;
7+
8+
bool ok = false;
9+
int fact[1000001], cnt[1000001];
10+
// This factors can be optimized using prime sieve method
11+
// void factors(int x) {
12+
// if (ok)
13+
// return;
14+
// for (int i = 1; i * i <= x; i++) {
15+
// if (x % i == 0) {
16+
// int a = i, b = x / i;
17+
// if (a == b) {
18+
// a = i;
19+
// b = 1;
20+
// }
21+
// if (a != 1) {
22+
// if (fact[a]) {
23+
// ok = true;
24+
// return;
25+
// }
26+
// fact[a] = true;
27+
// }
28+
// if (b != 1) {
29+
// if (fact[b]) {
30+
// ok = true;
31+
// return;
32+
// }
33+
// fact[b] = true;
34+
// }
35+
// }
36+
// }
37+
// }
38+
39+
void sieve() {
40+
int n = 1000000;
41+
for (int i = 1; i <= n; i++)
42+
fact[i] = i;
43+
44+
for (int i = 2; i <= n; i++) {
45+
if (fact[i] == i) {
46+
for (int j = i; j <= n; j += i)
47+
fact[j] = i;
48+
}
49+
}
50+
}
51+
52+
53+
int32_t main() {
54+
#ifndef ONLINE_JUDGE
55+
freopen("input.txt", "r", stdin);
56+
freopen("output.txt", "w", stdout);
57+
#endif
58+
fastio;
59+
60+
sieve();
61+
int n;
62+
cin >> n;
63+
int arr[n], maxx = 0;
64+
int gcd = 0;
65+
for (int i = 0; i < n; i++) {
66+
cin >> arr[i];
67+
maxx = max(arr[i], maxx);
68+
gcd = __gcd(gcd, arr[i]);
69+
}
70+
bool flag = false;
71+
for (int i = 0; i < n; i++) {
72+
set<int> cur;
73+
while (arr[i] != 1) {
74+
cur.insert(fact[arr[i]]);
75+
arr[i] /= fact[arr[i]];
76+
}
77+
for (auto c : cur) {
78+
cnt[c]++;
79+
if (cnt[c] > 1) {
80+
flag = true;
81+
break;
82+
}
83+
}
84+
if (flag)
85+
break;
86+
}
87+
if (!flag)
88+
cout << "pairwise coprime";
89+
else if (gcd == 1)
90+
cout << "setwise coprime";
91+
else
92+
cout << "not coprime";
93+
return 0;
94+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
#define N 101
7+
int arr[100001], pref[100001], suff[100001];
8+
9+
// https://www.codechef.com/problems/GCDQ
10+
// Exclude L and R part from gcd and calculate gcd for rest array
11+
// Idea is to prepare prefix and suffix array of gcds and take gcd of pref[l - 1] and suff[r + 1]
12+
13+
int mod = 1e9 + 7;
14+
15+
int gcd(int a, int b) {
16+
while(b) {
17+
int temp = b;
18+
b = a % b;
19+
a = temp;
20+
}
21+
return a;
22+
}
23+
24+
int32_t main() {
25+
int t, q, l, r, n;
26+
cin >> t;
27+
while(t--) {
28+
cin >> n >> q;
29+
for(int i = 1; i <= n; i++)
30+
cin >> arr[i];
31+
32+
pref[0] = 0;
33+
for(int i = 1; i <= n; i++)
34+
pref[i] = gcd(pref[i - 1], arr[i]);
35+
36+
suff[n + 1] = 0;
37+
for(int i = n; i >= 1; i--)
38+
suff[i] = gcd(suff[i + 1], arr[i]);
39+
40+
while(q--) {
41+
cin >> l >> r;
42+
cout << gcd(pref[l - 1], suff[r + 1]) << endl;
43+
}
44+
}
45+
return 0;
46+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
int mod = 1e9 + 7;
7+
8+
// https://www.codechef.com/problems/GCDMOD
9+
// As here we have to find gcd of a ^ n + b ^ n and |a - b| so its good to find candidates for second than first as a - b would have less values to check
10+
// calculating a ^ n + b ^ n can be huge but here we are using the number we got from candidates of a - b as mod for a ^ n and b ^ n that would reduce the calculations
11+
12+
int power(int a, int n, int m) {
13+
int res = 1;
14+
while(n) {
15+
if(n & 1) {
16+
res = ((res % m) * (a % m)) % m;
17+
n--;
18+
} else {
19+
a = ((a % m) * (a % m)) % m;
20+
n /= 2;
21+
}
22+
}
23+
return res;
24+
}
25+
26+
int gcd(int a, int b, int n) {
27+
// a == b then a - b == 0 so we are adding power of a and b
28+
if(a == b)
29+
return (power(a, n, mod) + power(b, n, mod)) % mod;
30+
31+
// here we will find candidates of a - b till sqrt(a - b) and we will choose maximum candidate that divides a - b
32+
int num = a - b;
33+
int candidate = 1;
34+
for(int i = 1; i * i <= num; i++) {
35+
if(num % i == 0) {
36+
int temp = (power(a, n, i) + power(b, n, i)) % i;
37+
if(temp == 0)
38+
candidate = max(candidate, i);
39+
temp = (power(a, n, num / i) + power(b, n, num / i)) % (num / i);
40+
if(temp == 0)
41+
candidate = max(candidate, num / i);
42+
}
43+
}
44+
return candidate % mod;
45+
}
46+
47+
48+
int32_t main() {
49+
int t, a, b, n;
50+
cin >> t;
51+
while(t--) {
52+
cin >> a >> b >> n;
53+
cout << gcd(a, b, n) << endl;
54+
}
55+
return 0;
56+
}

Number Theory/(CODEFORCES)742A.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
7+
int power(int a, int n, int p) {
8+
int res = 1;
9+
while(n) {
10+
if(n & 1) {
11+
res = (res * a) % p;
12+
n--;
13+
} else {
14+
a = (a * a) % p;
15+
n /= 2;
16+
}
17+
}
18+
return res;
19+
}
20+
21+
int32_t main() {
22+
int n;
23+
cin >> n;
24+
// 8 is congruence to 1378
25+
cout << power(8, n, 10);
26+
return 0;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
#define N 3
7+
int T[N][N], I[N][N], arr[N];
8+
9+
// https://www.hackerrank.com/challenges/fibonacci-finding-easy/problem
10+
11+
int mod = 1e9 + 7;
12+
13+
void mul(int A[][N], int B[][N], int dim) {
14+
int res[dim + 1][dim + 1];
15+
for(int i = 1; i <= dim; i++) {
16+
for(int j = 1; j <= dim; j++) {
17+
res[i][j] = 0;
18+
for(int k = 1; k <= dim; k++)
19+
res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % mod;
20+
}
21+
}
22+
for(int i = 1; i <= dim; i++)
23+
for(int j = 1; j <= dim; j++)
24+
A[i][j] = res[i][j];
25+
}
26+
27+
int getFib(int n) {
28+
if(n <= 2)
29+
return arr[n];
30+
31+
// Identity Matrix
32+
I[1][1] = I[2][2] = 1;
33+
I[1][2] = I[2][1] = 0;
34+
35+
// Transition Matrix for sequence
36+
T[1][1] = 0;
37+
T[1][2] = T[2][1] = T[2][2] = 1;
38+
39+
// as transition matrix raise to n - 1 for nth term
40+
n--;
41+
42+
while(n) {
43+
if(n & 1)
44+
mul(I, T, 2), n--;
45+
else
46+
mul(T, T, 2), n /= 2;
47+
}
48+
return (arr[1] * I[1][1] + arr[2] * I[2][1]) % mod;
49+
}
50+
51+
52+
int32_t main() {
53+
int t, n;
54+
cin >> t;
55+
while(t--) {
56+
cin >> arr[1] >> arr[2] >> n;
57+
n++;
58+
cout << getFib(n) << endl;
59+
}
60+
return 0;
61+
}
62+

Number Theory/BinomialCoefficient.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
int fact[1000001];
7+
int mod = 1e9 + 7;
8+
9+
// Calculate C(n, k) % mod where mod > n
10+
// formula is n! / k! * (n - k)! but here we are using mod and precalculating factorial
11+
12+
int power(int a, int n) {
13+
int res = 1;
14+
while(n) {
15+
if(n & 1)
16+
res = (res * a) % mod, n--;
17+
else
18+
a = (a * a) % mod, n /= 2;
19+
}
20+
return res;
21+
}
22+
23+
int C(int n, int k) {
24+
if(k > n)
25+
return 0;
26+
int res = fact[n];
27+
// here instead of dividing we use inverse
28+
res = (res * power(fact[k], mod - 2)) % mod;
29+
res = (res * power(fact[n - k], mod - 2)) % mod;
30+
return res;
31+
}
32+
33+
int32_t main() {
34+
int t, n, k;
35+
cin >> t;
36+
fact[0] = fact[1] = 1;
37+
38+
for(int i = 2; i <= 1000000; i++)
39+
fact[i] = (fact[i - 1] * i) % mod;
40+
41+
while(t--) {
42+
cin >> n >> k;
43+
cout << C(n, k) << endl;
44+
}
45+
return 0;
46+
}

Number Theory/FermatsInverse.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
7+
int power(int a, int n, int m) {
8+
int res = 1;
9+
while(n) {
10+
if(n & 1) {
11+
res = (res * a) % m;
12+
n--;
13+
} else {
14+
a = (a * a) % m;
15+
n /= 2;
16+
}
17+
}
18+
return res;
19+
}
20+
21+
int32_t main() {
22+
int t, a, m;
23+
cin >> t;
24+
while(t--) {
25+
cin >> a >> m;
26+
cout << power(a, m - 2, m) << endl;
27+
}
28+
return 0;
29+
}

Number Theory/PrimeFactorization.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<bits/stdc++.h>
2+
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
3+
#define endl "\n"
4+
#define int long long int
5+
using namespace std;
6+
7+
// Complexity - O(sqrt(N))
8+
void primeFact(int n) {
9+
for(int i = 2; i * i <= n; i++) {
10+
if(n % i == 0) {
11+
int cnt = 0;
12+
while(n % i == 0) {
13+
cnt++;
14+
n /= i;
15+
}
16+
cout << i << "^" << cnt << endl;
17+
}
18+
}
19+
// If number is prime then it would need itself raise to 1 as no number would divide it
20+
if(n > 1)
21+
cout << n << "^" << 1 << endl;
22+
}
23+
24+
int32_t main() {
25+
int n;
26+
cin >> n;
27+
primeFact(n);
28+
return 0;
29+
}

0 commit comments

Comments
 (0)