Skip to content

Commit c5f45f3

Browse files
authored
Merge branch 'main' into main
2 parents f4f5552 + 7e6041d commit c5f45f3

5 files changed

Lines changed: 252 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(int argc, char const *argv[])
5+
{
6+
int t;
7+
cin >> t;
8+
while (t--)
9+
{
10+
long long int a, b, c;
11+
cin >> a >> b >> c;
12+
if ((c <= abs(b - a) * 2) && (abs(b - a) >= min(a, b)))
13+
{
14+
if (c + abs(b - a) <= abs(b - a) * 2)
15+
{
16+
cout << c + abs(b - a) << endl;
17+
}
18+
else
19+
{
20+
cout << c - abs(b - a) << endl;
21+
}
22+
}
23+
else
24+
{
25+
cout << -1 << endl;
26+
}
27+
}
28+
29+
return 0;
30+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
====================================================
3+
PROBLEM STATEMENT:
4+
====================================================
5+
You are given an integer n and an integer k (k ≥ 2),
6+
along with an array of n positive integers.
7+
8+
Your task is to count the number of unordered pairs
9+
(i, j) such that:
10+
- 1 ≤ i < j ≤ n
11+
- ai × aj is a perfect k-th power
12+
13+
A number is a perfect k-th power if it can be written
14+
as x^k for some integer x.
15+
16+
----------------------------------------------------
17+
PROBLEM LINK:
18+
https://codeforces.com/problemset/problem/1225/D
19+
====================================================
20+
*/
21+
22+
/*
23+
====================================================
24+
APPROACH:
25+
====================================================
26+
Key Idea:
27+
For a product ai × aj to be a perfect k-th power,
28+
the total exponent of every prime factor in the
29+
product must be divisible by k.
30+
31+
Steps:
32+
1. For each number ai, perform prime factorization.
33+
2. Reduce each prime’s exponent modulo k.
34+
3. Store the resulting factorization as "cur".
35+
4. Compute the complementary factorization "need"
36+
such that:
37+
cur × need → all exponents divisible by k
38+
5. Use a map to count how many times "need"
39+
has appeared before.
40+
6. Add that count to the answer.
41+
7. Insert "cur" into the map.
42+
43+
----------------------------------------------------
44+
WHY THIS WORKS:
45+
- Pairing numbers whose prime exponents complement
46+
each other modulo k ensures the product becomes
47+
a perfect k-th power.
48+
====================================================
49+
*/
50+
51+
/*
52+
====================================================
53+
TIME & SPACE COMPLEXITY:
54+
====================================================
55+
Time Complexity:
56+
O(n * sqrt(ai)) (prime factorization per element)
57+
58+
Space Complexity:
59+
O(n) (map storage)
60+
====================================================
61+
*/
62+
63+
/*
64+
====================================================
65+
EXAMPLE:
66+
====================================================
67+
Input:
68+
6 3
69+
1 3 9 8 24 1
70+
71+
Output:
72+
5
73+
74+
Explanation:
75+
There are 5 unordered pairs whose product is a
76+
perfect cube.
77+
====================================================
78+
*/
79+
80+
/*
81+
====================================================
82+
SUBMISSION LINK:
83+
====================================================
84+
(Add your Codeforces submission link here after AC)
85+
====================================================
86+
*/
87+
88+
#include <bits/stdc++.h>
89+
using namespace std;
90+
91+
#define fastio() ios::sync_with_stdio(false); cin.tie(nullptr);
92+
93+
int main() {
94+
fastio();
95+
96+
int n, k;
97+
cin >> n >> k;
98+
99+
vector<int> a(n);
100+
for (int i = 0; i < n; i++) {
101+
cin >> a[i];
102+
}
103+
104+
map<vector<pair<int,int>>, long long> mp;
105+
long long ans = 0;
106+
107+
for (int x : a) {
108+
vector<pair<int,int>> cur, need;
109+
110+
int temp = x;
111+
for (int p = 2; p * p <= temp; p++) {
112+
if (temp % p == 0) {
113+
int cnt = 0;
114+
while (temp % p == 0) {
115+
temp /= p;
116+
cnt++;
117+
}
118+
cnt %= k;
119+
if (cnt) {
120+
cur.push_back({p, cnt});
121+
need.push_back({p, (k - cnt) % k});
122+
}
123+
}
124+
}
125+
126+
if (temp > 1) {
127+
int cnt = 1 % k;
128+
if (cnt) {
129+
cur.push_back({temp, cnt});
130+
need.push_back({temp, (k - cnt) % k});
131+
}
132+
}
133+
134+
sort(cur.begin(), cur.end());
135+
sort(need.begin(), need.end());
136+
137+
ans += mp[need];
138+
mp[cur]++;
139+
}
140+
141+
cout << ans << '\n';
142+
return 0;
143+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
int t;
6+
cin >> t;
7+
vector<int> results(t);
8+
9+
for(int i = 0; i < t; i++){
10+
int n;
11+
cin >> n;
12+
13+
vector<int> arr(n);
14+
for (int j = 0; j < n; j++){
15+
cin >> arr[j];
16+
}
17+
18+
int max = 0;
19+
for(int x = 0; x < n; x++){
20+
for(int y = 0; y < n; y++){
21+
if(abs(arr[x]-arr[y])>=max){
22+
max=abs(arr[x]-arr[y]);
23+
}
24+
}
25+
}
26+
int count = 0;
27+
for(int x = 0; x < n; x++){
28+
for(int y = 0; y < n; y++){
29+
if(abs(arr[x] - arr[y]) == max){
30+
count++;
31+
}
32+
}
33+
}
34+
results[i] = count;
35+
}
36+
for(int i = 0; i < t; i++){
37+
cout << results[i] << endl;
38+
}
39+
return 0;
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//https://codeforces.com/problemset/submission/1771/355389933
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
int main() {
7+
int t;
8+
cin >> t;
9+
10+
while (t--) {
11+
int n;
12+
cin >> n;
13+
14+
long long a[n];
15+
for (int i = 0; i < n; i++)
16+
cin >> a[i];
17+
18+
long long mn = a[0], mx = a[0];
19+
for (int i = 1; i < n; i++) {
20+
mn = min(mn, a[i]);
21+
mx = max(mx, a[i]);
22+
}
23+
24+
long long cmin = 0, cmax = 0;
25+
for (int i = 0; i < n; i++) {
26+
if (a[i] == mn) cmin++;
27+
if (a[i] == mx) cmax++;
28+
}
29+
30+
if (mn == mx)
31+
cout << n * (n - 1) << "\n";
32+
else
33+
cout << 2 * cmin * cmax << "\n";
34+
}
35+
36+
return 0;
37+
}

contributers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,7 @@
7373
|Aman Sharma | | amansharma264 |Sir M Visvesvaraya Institute of Technology |
7474
| Aaryan Degama | Aaryan-Degama | IIIT Allahabad |
7575
| Sachin Benakannavar | sachinbenakannavar696 | IIIT Allahabad |
76+
| Aaryan Degama | Aaryan-Degama | IIIT Allahabad
77+
|Aditi Deshmukh | InsanelySlowBurn | IIIT Allahabad |
7678
<!-- ADD ABOVE THIS -->
7779
<!-- example | Korvac | Betty | Reyansh College | -->

0 commit comments

Comments
 (0)