File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-01/sol/BEESA_MANISH Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ n = number of elements (≤ 100,000)
3+
4+ A = maximum value of ai (≤ 100,000)
5+
6+ Space Complexity - 𝑂(𝐴+𝑛)
7+
8+ Time Complexity - O(nlogA)
9+
10+
11+ */
12+
13+
14+ #include < bits/stdc++.h>
15+ using namespace std ;
16+
17+ int main () {
18+ ios::sync_with_stdio (false );
19+ cin.tie (nullptr );
20+
21+ int n, k;
22+ cin >> n >> k;
23+
24+ vector<int > a (n);
25+ for (int i = 0 ; i < n; i++) cin >> a[i];
26+
27+ // Smallest Prime Factor sieve
28+ const int MAXA = 100000 ;
29+ vector<int > spf (MAXA + 1 );
30+ for (int i = 1 ; i <= MAXA; i++) spf[i] = i;
31+ for (int i = 2 ; i * i <= MAXA; i++) {
32+ if (spf[i] == i) {
33+ for (int j = i * i; j <= MAXA; j += i)
34+ if (spf[j] == j)
35+ spf[j] = i;
36+ }
37+ }
38+
39+ map<vector<pair<int ,int >>, long long > freq;
40+ long long ans = 0 ;
41+
42+ for (int x : a) {
43+ map<int ,int > cnt;
44+
45+ // factorization
46+ while (x > 1 ) {
47+ int p = spf[x];
48+ int c = 0 ;
49+ while (x % p == 0 ) {
50+ x /= p;
51+ c++;
52+ }
53+ cnt[p] += c;
54+ }
55+
56+ vector<pair<int ,int >> sig, need;
57+
58+ for (auto &it : cnt) {
59+ int p = it.first ;
60+ int e = it.second % k;
61+ if (e != 0 ) {
62+ sig.push_back ({p, e});
63+ need.push_back ({p, (k - e) % k});
64+ }
65+ }
66+
67+ sort (sig.begin (), sig.end ());
68+ sort (need.begin (), need.end ());
69+
70+ ans += freq[need];
71+ freq[sig]++;
72+ }
73+
74+ cout << ans << " \n " ;
75+ return 0 ;
76+ }
You can’t perform that action at this time.
0 commit comments