|
| 1 | +/* |
| 2 | +==================================================== |
| 3 | +PROBLEM STATEMENT: |
| 4 | +==================================================== |
| 5 | +You are given an integer n and an integer k (k ≥ 2), |
| 6 | +along with an array of n positive integers. |
| 7 | +
|
| 8 | +Your task is to count the number of unordered pairs |
| 9 | +(i, j) such that: |
| 10 | +- 1 ≤ i < j ≤ n |
| 11 | +- ai × aj is a perfect k-th power |
| 12 | +
|
| 13 | +A number is a perfect k-th power if it can be written |
| 14 | +as x^k for some integer x. |
| 15 | +
|
| 16 | +---------------------------------------------------- |
| 17 | +PROBLEM LINK: |
| 18 | +https://codeforces.com/problemset/problem/1225/D |
| 19 | +==================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +/* |
| 23 | +==================================================== |
| 24 | +APPROACH: |
| 25 | +==================================================== |
| 26 | +Key Idea: |
| 27 | +For a product ai × aj to be a perfect k-th power, |
| 28 | +the total exponent of every prime factor in the |
| 29 | +product must be divisible by k. |
| 30 | +
|
| 31 | +Steps: |
| 32 | +1. For each number ai, perform prime factorization. |
| 33 | +2. Reduce each prime’s exponent modulo k. |
| 34 | +3. Store the resulting factorization as "cur". |
| 35 | +4. Compute the complementary factorization "need" |
| 36 | + such that: |
| 37 | + cur × need → all exponents divisible by k |
| 38 | +5. Use a map to count how many times "need" |
| 39 | + has appeared before. |
| 40 | +6. Add that count to the answer. |
| 41 | +7. Insert "cur" into the map. |
| 42 | +
|
| 43 | +---------------------------------------------------- |
| 44 | +WHY THIS WORKS: |
| 45 | +- Pairing numbers whose prime exponents complement |
| 46 | + each other modulo k ensures the product becomes |
| 47 | + a perfect k-th power. |
| 48 | +==================================================== |
| 49 | +*/ |
| 50 | + |
| 51 | +/* |
| 52 | +==================================================== |
| 53 | +TIME & SPACE COMPLEXITY: |
| 54 | +==================================================== |
| 55 | +Time Complexity: |
| 56 | +O(n * sqrt(ai)) (prime factorization per element) |
| 57 | +
|
| 58 | +Space Complexity: |
| 59 | +O(n) (map storage) |
| 60 | +==================================================== |
| 61 | +*/ |
| 62 | + |
| 63 | +/* |
| 64 | +==================================================== |
| 65 | +EXAMPLE: |
| 66 | +==================================================== |
| 67 | +Input: |
| 68 | +6 3 |
| 69 | +1 3 9 8 24 1 |
| 70 | +
|
| 71 | +Output: |
| 72 | +5 |
| 73 | +
|
| 74 | +Explanation: |
| 75 | +There are 5 unordered pairs whose product is a |
| 76 | +perfect cube. |
| 77 | +==================================================== |
| 78 | +*/ |
| 79 | + |
| 80 | +/* |
| 81 | +==================================================== |
| 82 | +SUBMISSION LINK: |
| 83 | +==================================================== |
| 84 | +(Add your Codeforces submission link here after AC) |
| 85 | +==================================================== |
| 86 | +*/ |
| 87 | + |
| 88 | +#include <bits/stdc++.h> |
| 89 | +using namespace std; |
| 90 | + |
| 91 | +#define fastio() ios::sync_with_stdio(false); cin.tie(nullptr); |
| 92 | + |
| 93 | +int main() { |
| 94 | + fastio(); |
| 95 | + |
| 96 | + int n, k; |
| 97 | + cin >> n >> k; |
| 98 | + |
| 99 | + vector<int> a(n); |
| 100 | + for (int i = 0; i < n; i++) { |
| 101 | + cin >> a[i]; |
| 102 | + } |
| 103 | + |
| 104 | + map<vector<pair<int,int>>, long long> mp; |
| 105 | + long long ans = 0; |
| 106 | + |
| 107 | + for (int x : a) { |
| 108 | + vector<pair<int,int>> cur, need; |
| 109 | + |
| 110 | + int temp = x; |
| 111 | + for (int p = 2; p * p <= temp; p++) { |
| 112 | + if (temp % p == 0) { |
| 113 | + int cnt = 0; |
| 114 | + while (temp % p == 0) { |
| 115 | + temp /= p; |
| 116 | + cnt++; |
| 117 | + } |
| 118 | + cnt %= k; |
| 119 | + if (cnt) { |
| 120 | + cur.push_back({p, cnt}); |
| 121 | + need.push_back({p, (k - cnt) % k}); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (temp > 1) { |
| 127 | + int cnt = 1 % k; |
| 128 | + if (cnt) { |
| 129 | + cur.push_back({temp, cnt}); |
| 130 | + need.push_back({temp, (k - cnt) % k}); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + sort(cur.begin(), cur.end()); |
| 135 | + sort(need.begin(), need.end()); |
| 136 | + |
| 137 | + ans += mp[need]; |
| 138 | + mp[cur]++; |
| 139 | + } |
| 140 | + |
| 141 | + cout << ans << '\n'; |
| 142 | + return 0; |
| 143 | +} |
0 commit comments