Skip to content

Commit a9334e5

Browse files
authored
Merge pull request #238 from suzzzal/patch-3
day01sol02
2 parents 9bc2bcd + 608d6d5 commit a9334e5

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
submission link - https://codeforces.com/contest/1225/submission/355299661
3+
n = number of elements (≤ 100000)
4+
A = maximum value of ai (≤ 100000)
5+
6+
Time Complexity:
7+
- Sieve creation: O(A log log A)
8+
- Factorization of all elements: O(n log A)
9+
Overall: O(n log A)
10+
11+
Space Complexity:
12+
- SPF array + maps: O(A + n)
13+
14+
Approach:
15+
We want to count pairs of numbers whose product is a perfect k-th power.
16+
17+
For each number:
18+
1. Factorize it using the Smallest Prime Factor (SPF) sieve.
19+
2. Keep prime exponents modulo k to form a signature.
20+
3. Compute the required signature needed to complete k.
21+
4. Count how many times this required signature appeared before.
22+
5. Add it to the answer and store the current signature.
23+
*/
24+
25+
#include<bits/stdc++.h>
26+
using namespace std;
27+
28+
int main(){
29+
int size,power;
30+
cin>>size>>power;
31+
32+
vector<int>arr(size);
33+
for(int i=0;i<size;i++)cin>>arr[i];
34+
35+
const int limit=100000;
36+
vector<int>spf(limit+1);
37+
for(int i=1;i<=limit;i++)spf[i]=i;
38+
for(int i=2;i*i<=limit;i++){
39+
if(spf[i]==i){
40+
for(int j=i*i;j<=limit;j+=i){
41+
if(spf[j]==j)spf[j]=i;
42+
}
43+
}
44+
}
45+
46+
map<vector<pair<int,int>>,long long>store;
47+
long long result=0;
48+
49+
for(int val:arr){
50+
map<int,int>count;
51+
while(val>1){
52+
int prime=spf[val];
53+
int exp=0;
54+
while(val%prime==0){
55+
val/=prime;
56+
exp++;
57+
}
58+
count[prime]+=exp;
59+
}
60+
61+
vector<pair<int,int>>sign,need;
62+
for(auto &it:count){
63+
int prime=it.first;
64+
int rem=it.second%power;
65+
if(rem!=0){
66+
sign.push_back({prime,rem});
67+
need.push_back({prime,(power-rem)%power});
68+
}
69+
}
70+
71+
sort(sign.begin(),sign.end());
72+
sort(need.begin(),need.end());
73+
74+
result+=store[need];
75+
store[sign]++;
76+
}
77+
78+
cout<<result<<"\n";
79+
return 0;
80+
}

0 commit comments

Comments
 (0)