Skip to content

Commit 2fed0be

Browse files
committed
solved q2
1 parent ecd299e commit 2fed0be

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
static const int maxi = 100000;
5+
int spf[maxi + 1];
6+
int k;
7+
8+
/*sieve*/
9+
void buildSPF() {
10+
for (int i = 1; i <= maxi; i++)
11+
spf[i] = i;
12+
13+
for (int i = 2; i * i <= maxi; i++) {
14+
if (spf[i] == i) {
15+
for (int j = i * i; j <= maxi; j += i) {
16+
if (spf[j] == j)
17+
spf[j] = i;
18+
}
19+
}
20+
}
21+
}
22+
23+
/*factorise*/
24+
vector<pair<int,int>> getSignature(int x) {
25+
map<int,int> factors;
26+
27+
while (x > 1) {
28+
int p = spf[x];
29+
factors[p]++;
30+
x /= p;
31+
}
32+
33+
vector<pair<int,int>> sig;
34+
for (auto &it : factors) {
35+
int exp = it.second % k;
36+
if (exp != 0)
37+
sig.push_back({it.first, exp});
38+
}
39+
return sig;
40+
}
41+
42+
/*how many needed*/
43+
vector<pair<int,int>> getComplement(const vector<pair<int,int>> &sig) {
44+
vector<pair<int,int>> comp;
45+
for (auto &p : sig) {
46+
comp.push_back({p.first, (k - p.second) % k});
47+
}
48+
return comp;
49+
}
50+
51+
int main() {
52+
ios::sync_with_stdio(false);
53+
cin.tie(nullptr);
54+
55+
int n;
56+
cin >> n >> k;
57+
58+
vector<int> a(n);
59+
for (int i = 0; i < n; i++)
60+
cin >> a[i];
61+
62+
buildSPF();
63+
64+
map<vector<pair<int,int>>, long long> freq;
65+
long long ans = 0;
66+
67+
for (int i = 0; i < n; i++) {
68+
vector<pair<int,int>> sig = getSignature(a[i]);
69+
vector<pair<int,int>> need = getComplement(sig);
70+
ans += freq[need];
71+
freq[sig]++;
72+
}
73+
cout << ans << "\n";
74+
return 0;
75+
}
76+
//https://codeforces.com/problemset/submission/1225/355302622
77+
//time complexity O(nlognlog(10^5))

0 commit comments

Comments
 (0)