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.c++

Input

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

Output

Print a single integer — the number of suitable pairs.翻譯

Example

input
6 3
1 3 9 8 24 1
output
5code

Note

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;
}
相關文章
相關標籤/搜索