zoukankan      html  css  js  c++  java
  • Codeforces Round #596 D. Power Products 暴力 stl

    D. Power Products

    time limit per test

    2 seconds

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    You are given nn positive integers a1,…,ana1,…,an, and an integer k≥2k≥2. Count the number of pairs i,ji,j such that 1≤i<j≤n1≤i<j≤n, and there exists an integer xx such that ai⋅aj=xkai⋅aj=xk.

    Input

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

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

    Output

    Print a single integer — the number of suitable pairs.

    Example

    input

    Copy

    6 3
    1 3 9 8 24 1
    

    output

    Copy

    5
    

    Note

    In the sample case, the suitable pairs are:

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

    我写了很暴力的方法

    分解质因子,然后计算出每个数要构成k次方数,所需的剩余因子个数

    然后映射一个数量 每次查询map

    用一个map<vector<pair<int,int> >,int>  来对应每个数所需要的因子和个数即可

    #include<bits/stdc++.h>
    using namespace std;
    map<vector<pair<int,int> >, int>mp;
    int main()
    {
        int n,k;
        scanf("%d%d",&n,&k);
        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            vector<pair<int,int> >ve;
            ve.clear();
            int tmp,temp;
            scanf("%d",&tmp);
            ve.push_back(make_pair(1,1));
            temp=sqrt(tmp);
            for(int j=2;j<=temp;j++)
            {
                int num=0;
                while(tmp%j==0)
                {
                    tmp/=j;
                    num++;
                }
                num%=k;
                if(num!=0)
                {
                    ve.push_back(make_pair(j,num));
                    //cout<<j<<" "<<num<<endl;
                }
            }
            if(tmp!=1)
            {
                ve.push_back(make_pair(tmp,1));
                //cout<<tmp<<" "<<1<<endl;
            }
            ans+=mp[ve];
            for(int j=1;j<ve.size();j++)
            {
                ve[j].second=k-ve[j].second;
            }
            //cout<<ve[1].second<<endl;
            mp[ve]++;
            //cout<<ans<<endl;
        }
        printf("%I64d
    ",ans);
  • 相关阅读:
    13.Convert BST to Greater Tree(将树转为更大树)
    13.调用数组顺序使奇数位于偶数前面
    12.数值的整数次方
    11.二进制中1的个数
    12.Hamming Distance(汉明距离)
    11.Find All Numbers Disappeared in an Array(找出数组中缺失的数)
    10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)
    垃圾收集器与内存分配策略---垃圾收集器
    线程之间的协作
    1287. Mars Canals(DP)
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852200.html
Copyright © 2011-2022 走看看