Skip to content

Commit d165d5f

Browse files
Add files via upload
1 parent da6a7e2 commit d165d5f

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

  • Problems/Mathematics/Day-01/sol/Aiyaan_Mahajan
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Q2 You are given 𝑛
3+
positive integers 𝑎1,…,𝑎𝑛
4+
, and an integer 𝑘≥2
5+
. Count the number of pairs 𝑖,𝑗
6+
such that 1≤𝑖<𝑗≤𝑛
7+
, and there exists an integer 𝑥
8+
such that 𝑎𝑖⋅𝑎𝑗=𝑥𝑘
9+
.*/
10+
11+
#include <bits/stdc++.h>
12+
using namespace std;
13+
14+
// Function to factorize a number and return prime exponents modulo k
15+
map<int, int> factorizeMod(long long x, int k) {
16+
map<int, int> factors;
17+
for (long long p = 2; p * p <= x; ++p) {
18+
int count = 0;
19+
while (x % p == 0) {
20+
x /= p;
21+
count++;
22+
}
23+
if (count % k != 0) {
24+
factors[p] = count % k; // only store modulo k
25+
}
26+
}
27+
if (x > 1) factors[x] = 1 % k; // remaining prime factor
28+
return factors;
29+
}
30+
31+
// Function to compute the complement signature modulo k
32+
map<int, int> complementSignature(const map<int, int>& sig, int k) {
33+
map<int, int> comp;
34+
for (auto &[prime, exp] : sig) {
35+
comp[prime] = (k - exp) % k;
36+
}
37+
return comp;
38+
}
39+
40+
int main() {
41+
42+
int n, k;
43+
cin >> n >> k;
44+
vector<long long> a(n);
45+
for (int i = 0; i < n; ++i) cin >> a[i];
46+
47+
// Map to count how many times each signature has occurred
48+
map<map<int,int>, long long> signatureCount;
49+
long long totalPairs = 0;
50+
51+
for (int i = 0; i < n; ++i) {
52+
map<int,int> sig = factorizeMod(a[i], k);
53+
map<int,int> comp = complementSignature(sig, k);
54+
55+
// Add pairs that match the complement
56+
totalPairs += signatureCount[comp];
57+
58+
// Increment count of current signature
59+
signatureCount[sig]++;
60+
}
61+
62+
cout << totalPairs << "\n";
63+
return 0;
64+
}
65+
66+
/*
67+
My submission : https://codeforces.com/problemset/submission/1225/355325134
68+
*/

0 commit comments

Comments
 (0)