Skip to content

Commit b9da529

Browse files
authored
Merge pull request #328 from Naman2251/main
Create day001sol02.cpp
2 parents 3a582d7 + 177152b commit b9da529

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Submission Link: https://codeforces.com/contest/1225/submission/355608300
2+
3+
/*
4+
So first I made a vector t which stores data of type pair(int, int) and a map c from vector storing pair(int, int) to int. Now I take input from user each element
5+
of array a given as input. Now, I do prime factorisation of the input number and if the power is not a multiple of k then I store it in vector t as
6+
pair(prime no., power%k). After the loop runs up to the square root of a, if a is still not 1, then the remaining value is a prime number, so we store the pair (a, 1).
7+
Then, I increment the value in the map c for the key t[i], which counts how many times this specific signature has appeared. Now in next for loop I made a vector s
8+
which also store pair(int, int). Now, I iterated through all the pair in each vector of t and added a pair(p.first, k - p.second) to vector s. This creates the
9+
"complement" signature—basically what prime factors and powers are missing to make a perfect k-th power. Then, I look up this vector s in the map c and add its
10+
frequency to ans. I also check if s is equal to t[i] (meaning the number is its own complement); if so, I decrement ans by 1 to avoid counting the number pairing
11+
with itself. Finally, I divide ans by 2 to remove double counting (since pair (A, B) and (B, A) are the same) and print the result.
12+
13+
Time Complexity: O(N * sqrt(V)), where N is the number of elements and V is the maximum value of an element (since we iterate up to sqrt(V) for factorization).
14+
Space Complexity: O(N), as we store the prime signatures for all N elements in the vector and map.
15+
*/
16+
#include <bits/stdc++.h>
17+
using namespace std;
18+
19+
int main() {
20+
int n, k;
21+
cin >> n >> k;
22+
23+
vector <pair <int, int> > t[n];
24+
map<vector<pair<int, int>>, int> c;
25+
26+
for (int i = 0; i < n; i++) {
27+
int a;
28+
cin >> a;
29+
for (int j = 2; j * j <= a; j++) {
30+
if (a % j == 0) {
31+
int p = 0;
32+
while (a % j == 0) {
33+
a /= j;
34+
p++;
35+
}
36+
if (p % k > 0) {
37+
t[i].push_back({j, p % k});
38+
}
39+
}
40+
}
41+
if (a != 1) {
42+
t[i].push_back({a, 1});
43+
}
44+
c[t[i]]++;
45+
}
46+
long long ans = 0;
47+
for (int i = 0; i < n; i++) {
48+
vector<pair<int, int>> s;
49+
for (pair<int, int> p : t[i]) {
50+
s.push_back({p.first, k - p.second});
51+
}
52+
ans += c[s];
53+
if (s == t[i]) {
54+
ans--;
55+
}
56+
}
57+
ans /= 2;
58+
cout << ans << endl;
59+
}

0 commit comments

Comments
 (0)