Skip to content

Commit e89ed92

Browse files
committed
add-day1-sol
1 parent 6d8d4e3 commit e89ed92

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Problem:
3+
People are standing in a circle numbered 1 to n (n is even).
4+
Person a is opposite person b.
5+
We must find who is opposite person c.
6+
If impossible, print -1.
7+
8+
Key Observation:
9+
If a is opposite b → distance between them is exactly n/2.
10+
So:
11+
n = |a - b| * 2
12+
13+
Once we know n, we know half circle size = n / 2
14+
15+
Opposite of any person x is:
16+
x + n/2 (if <= n)
17+
otherwise:
18+
x - n/2
19+
20+
Invalid Cases:
21+
1.max(a, b, c) > n → means c doesn't exist in circle
22+
2.a == b or diff == 0 → opposite cannot be same
23+
Output -1 in such cases.
24+
*/
25+
26+
#include <bits/stdc++.h>
27+
using namespace std;
28+
29+
int main() {
30+
int t;
31+
cin >> t;
32+
33+
while(t--){
34+
long long a, b, c;
35+
cin >> a >> b >> c;
36+
37+
// Distance between a and b
38+
long long diff = abs(a - b);
39+
40+
// Total number of people in circle
41+
long long n = diff * 2;
42+
43+
// Invalid cases
44+
if (diff == 0 || a > n || b > n || c > n) {
45+
cout << -1 << endl;
46+
continue;
47+
}
48+
49+
// Halfway around the circle
50+
long long half = n / 2;
51+
52+
// Opposite of c
53+
long long ans = c + half;
54+
55+
// If goes beyond circle wrap around
56+
if (ans > n) ans -= n;
57+
58+
cout << ans << endl;
59+
}
60+
return 0;
61+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
We count pairs (i, j) such that ai * aj is a perfect k-th power.
3+
4+
Idea:
5+
For each number, compute its prime factorization.
6+
Reduce each exponent modulo k because only residue matters.
7+
This forms a "signature".
8+
9+
Example (k = 3)
10+
24 = 2^3 * 3^1 → exponents mod 3 → 2^0 * 3^1
11+
12+
To pair with another number to form perfect k-th power,
13+
for each prime exponent 'e', we need (k - e) % k.
14+
15+
So for each number, we compute:
16+
signature = reduced exponents
17+
neededSignature = complementary exponents
18+
19+
We store counts of signatures in a map.
20+
For each number, answer += count[neededSignature].
21+
22+
Time Complexity:
23+
O(n * sqrt(max(ai)))
24+
25+
Space Complexity:
26+
O(n)
27+
*/
28+
29+
#include <bits/stdc++.h>
30+
using namespace std;
31+
32+
int main() {
33+
ios::sync_with_stdio(false);
34+
cin.tie(nullptr);
35+
36+
int n, k;
37+
cin >> n >> k;
38+
39+
vector<int> a(n);
40+
for (int &x : a) cin >> x;
41+
42+
map<vector<pair<int,int>>, long long> freq;
43+
long long ans = 0;
44+
45+
for (int x : a) {
46+
int num = x;
47+
map<int,int> cnt;
48+
49+
// Prime factorize
50+
for (int p = 2; p * p <= num; p++) {
51+
while (num % p == 0) {
52+
cnt[p]++;
53+
num /= p;
54+
}
55+
}
56+
if (num > 1) cnt[num]++;
57+
58+
vector<pair<int,int>> sig; // reduced signature
59+
vector<pair<int,int>> need; // complementary signature
60+
61+
for (auto &it : cnt) {
62+
int prime = it.first;
63+
int e = it.second % k;
64+
if (e != 0) {
65+
sig.push_back({prime, e});
66+
need.push_back({prime, (k - e) % k});
67+
}
68+
}
69+
70+
ans += freq[need];
71+
freq[sig]++;
72+
}
73+
74+
cout << ans << "\n";
75+
return 0;
76+
}

0 commit comments

Comments
 (0)