Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products 数学 暴力

D. Power Products

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.

Input

The first line contains two integers n and k (2≤n≤105, 2≤k≤100).

The second line contains n integers a1,…,an (1≤ai≤105).

Output

Print a single integer — the number of suitable pairs.

Example

input
6 3
1 3 9 8 24 1
output
5

Note

In the sample case, the suitable pairs are:

a1⋅a4=8=23;
a1⋅a6=1=13;
a2⋅a3=27=33;
a3⋅a5=216=63;
a4⋅a6=8=23.

题意

题目这么短,我就偷懒不翻译了吧。。

题解

首先我们质因数分解后,如果两个数的质因数分解后的每个数的因子个数都是k的倍数,那么就说明有解。

于是我们先对每个数质因数分解一下,然后再用一个vector去找一下配对的那一个是哪个。

代码

#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;
}
posted @ 2019-11-01 00:31  qscqesze  阅读(222)  评论(0编辑  收藏  举报