Skip to content

Commit 645ada0

Browse files
authored
Merge pull request #149 from ViMo018/patch-1
Add solution for counting divisible pairs
2 parents f3421f3 + 17824cc commit 645ada0

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+
#include<bits/stdc++.h>
2+
#include<queue>
3+
using namespace std;
4+
typedef unordered_map<int, int> umii;
5+
typedef unordered_map<long long, long long> umll;
6+
typedef unordered_map<char, long long> umci;
7+
typedef vector<pair<int, int>> vpi;
8+
typedef vector<int> vi;
9+
typedef long long ll;
10+
typedef vector<long long> vll;
11+
typedef unordered_map<int , bool> umib;
12+
#define sum(v) accumulate(v.begin(), v.end(), 0)
13+
#define endl '\n'
14+
#define f0(i, n) for(long long i = 0; i < n; i++)
15+
#define f1(i, n) for(long long i = 1; i < n; i++)
16+
#define as(v) sort(v.begin(), v.end())
17+
#define all(x) (x).begin(), (x).end()
18+
#define pb push_back
19+
template<class T> umll frequency(vector<T> &v) {umll freq;for(auto &x:v) freq[x]++; return freq;}
20+
template<class T> umci S_frequency(vector<T> &v) {umci freq;for(auto &x:v) freq[x]++; return freq;}
21+
template <class T> void input(vector<T> &v){for(auto &x:v)cin>>x;}
22+
ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; }
23+
void pvll(const vector<long long> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
24+
void pvi(const vector<int> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
25+
static const int MAXN = 100000;
26+
27+
28+
void solve(){
29+
30+
//-------------INPUT-------------
31+
ll n;
32+
cin >> n;
33+
ll k;
34+
cin >> k;
35+
vll v(n);
36+
input(v);
37+
//We need to count the no of pairs such that their product makes some number k- power means the product of the two numbers should have
38+
//factors frequency that is divisible by k
39+
vll spf(MAXN+1);
40+
for(ll i=1;i<=MAXN;i++)
41+
{
42+
spf[i]=i;
43+
}
44+
for(ll i=2;i*i<=MAXN;i++)
45+
{
46+
if(spf[i] == i)
47+
{
48+
for(ll j=i*i;j<=MAXN;j+=i){
49+
if(spf[j]==j)
50+
{
51+
spf[j] = i;
52+
}
53+
}
54+
}
55+
}
56+
map<vector<pair<ll,ll>>,ll>freq;
57+
58+
ll ans=0;
59+
60+
for(auto &x : v)
61+
{
62+
map<ll,ll>factor;
63+
while(x >1)
64+
{
65+
ll p = spf[x];
66+
ll cnt = 0;
67+
while(x%p ==0)
68+
{
69+
x = x/p;
70+
cnt++;
71+
}
72+
cnt=cnt%k;
73+
if(cnt>0)
74+
{
75+
factor[p] = cnt;
76+
}
77+
}
78+
vector<pair<ll,ll>>key,need;
79+
80+
for(auto &it : factor)
81+
{
82+
key.push_back(it);
83+
ll req = (k-it.second)%k;
84+
if(req>0)
85+
{
86+
need.push_back({it.first,req});
87+
}
88+
}
89+
ans+=freq[need];
90+
freq[key]++;
91+
}
92+
cout << ans << endl;
93+
94+
95+
//link submission :- https://codeforces.com/contest/1225/submission/355215715
96+
97+
//-------------CODE--------------
98+
99+
100+
101+
}
102+
103+
104+
int main(){
105+
//int tt; cin >> tt; while(tt--)
106+
{solve();};
107+
}

0 commit comments

Comments
 (0)