You are given n positive integers a1,…,an, and an integer k≥2. Count the number of pairs i,j such that 1≤i<j≤n, and there exists an integer x such that ai⋅aj=xk.c++
The first line contains two integers n and k (2≤n≤105, 2≤k≤100).ui
The second line contains n integers a1,…,an (1≤ai≤105).spa
Print a single integer — the number of suitable pairs.翻译
input
6 3
1 3 9 8 24 1
output
5code
In the sample case, the suitable pairs are:input
a1⋅a4=8=23;
a1⋅a6=1=13;
a2⋅a3=27=33;
a3⋅a5=216=63;
a4⋅a6=8=23.string
题目这么短,我就偷懒不翻译了吧。。it
首先咱们质因数分解后,若是两个数的质因数分解后的每一个数的因子个数都是k的倍数,那么就说明有解。table
因而咱们先对每一个数质因数分解一下,而后再用一个vector去找一下配对的那一个是哪一个。map
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; int n,k; int a[maxn]; map<vector<pair<int,int> >,int>H; int main(){ scanf("%d%d",&n,&k); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } long long ans = 0; for(int i=0;i<n;i++){ string tmp=""; vector<pair<int,int> >fac; for(int now=2;now*now<=a[i];now++){ int number=0; while(a[i]%now==0){ a[i]/=now; number+=1; } if(number%k) fac.push_back(make_pair(now,number%k)); } if(a[i]>1)fac.push_back(make_pair(a[i],1%k)); vector<pair<int,int> >fac2; for(int j=0;j<fac.size();j++){ fac2.push_back(make_pair(fac[j].first,k-fac[j].second)); } ans+=H[fac2]; H[fac]++; } cout<<ans<<endl; }