Skip to content

Commit df8d811

Browse files
authored
Merge pull request #73 from dwivediprashant/code/solution2
2 parents ab228ea + 59f711a commit df8d811

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Problem Statement:
3+
You are given an integer n and an integer k (k ≥ 2), along with an array of n
4+
positive integers.
5+
6+
Your task is to count the number of unordered index pairs (i, j) such that:
7+
1 ≤ i < j ≤ n
8+
The product ai × aj is a perfect k-th power.
9+
10+
---------------------------------------
11+
Approach:
12+
A number is a perfect k-th power if, in its prime factorization,
13+
the exponent of every prime is divisible by k.
14+
15+
For each number:
16+
1. Factorize it using smallest prime factor (SPF).
17+
2. Reduce each prime’s exponent modulo k.
18+
3. Store this reduced form as a "signature".
19+
20+
For a pair (ai, aj) to form a perfect k-th power:
21+
- Their signatures must complement each other such that
22+
the sum of exponents (mod k) becomes 0 for every prime.
23+
24+
---------------------------------------
25+
Time Complexity:
26+
O(n log a)
27+
28+
Space Complexity:
29+
O(n)
30+
---------------------------------------
31+
*/
32+
/*---------Problem link--------------------
33+
https://codeforces.com/problemset/status?my=on
34+
*/
35+
36+
import java.util.*;
37+
38+
public class Solution2 {
39+
40+
static int MAX = 100000;
41+
static int[] spf = new int[MAX + 1];
42+
43+
// Precompute smallest prime factor for every number
44+
static void computeSPF() {
45+
for (int i = 1; i <= MAX; i++)
46+
spf[i] = i;
47+
48+
for (int i = 2; i * i <= MAX; i++) {
49+
if (spf[i] == i) {
50+
for (int j = i * i; j <= MAX; j += i) {
51+
if (spf[j] == j)
52+
spf[j] = i;
53+
}
54+
}
55+
}
56+
}
57+
58+
// Build reduced prime-exponent signature
59+
static Map<Integer, Integer> buildSignature(int x, int k) {
60+
Map<Integer, Integer> map = new HashMap<>();
61+
while (x > 1) {
62+
int p = spf[x];
63+
int cnt = 0;
64+
while (x % p == 0) {
65+
x /= p;
66+
cnt++;
67+
}
68+
cnt %= k;
69+
if (cnt > 0)
70+
map.put(p, cnt);
71+
}
72+
return map;
73+
}
74+
75+
// Build complement signature
76+
static Map<Integer, Integer> buildComplement(Map<Integer, Integer> sig, int k) {
77+
Map<Integer, Integer> comp = new HashMap<>();
78+
for (Map.Entry<Integer, Integer> e : sig.entrySet()) {
79+
comp.put(e.getKey(), (k - e.getValue()) % k);
80+
}
81+
return comp;
82+
}
83+
84+
public static void main(String[] args) {
85+
Scanner sc = new Scanner(System.in);
86+
computeSPF();
87+
88+
int n = sc.nextInt();
89+
int k = sc.nextInt();
90+
91+
Map<Map<Integer, Integer>, Long> freq = new HashMap<>();
92+
long ans = 0;
93+
94+
for (int i = 0; i < n; i++) {
95+
int x = sc.nextInt();
96+
97+
Map<Integer, Integer> sig = buildSignature(x, k);
98+
Map<Integer, Integer> comp = buildComplement(sig, k);
99+
100+
ans += freq.getOrDefault(comp, 0L);
101+
freq.put(sig, freq.getOrDefault(sig, 0L) + 1);
102+
}
103+
104+
System.out.println(ans);
105+
sc.close();
106+
}
107+
}

0 commit comments

Comments
 (0)