Codeforces Round #596 div2 D. Power Products

时间:2019-10-28
本文章向大家介绍Codeforces Round #596 div2 D. Power Products,主要包括Codeforces Round #596 div2 D. Power Products使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

\(预先枚举1e10以内所有数的k次方,然后每一个ai都去找他所有的1e5以内的倍数,更新答案。\)
\(复杂度\sqrt[k]{1e10}\times{\sqrt{1e5}\times{2}}\)

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e5+100;
int n,k;
int a[N],vis[N];
ll b[N];
ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)ans=ans*a;
        if(ans>1e10||a>1e5)return 1e10+5;
        a=a*a;
        b>>=1;
    }
    return ans;
}
ll solve(int x)
{
    int cnt=0;
    ll res=1;
    for(int i=2;i*i<=x;i++)
    {
        cnt=0;
        while(x%i==0)
        {
            cnt++;
            x/=i;
        }
        if(cnt)res*=qpow(i,k-((cnt-1)%k+1));
        if(res>1e5+10)return res;
    }
    if(x!=1)res*=qpow(x,k-1);
    return res;
}
int main()
{
    //solve(40);
    scanf("%d%d",&n,&k);
    int cnt=0;
    for(int i=1;;i++)
    {
        ll p=qpow(i,k);
        if(p>1e10)break;
        b[++cnt]=p;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        ll p=solve(a[i]);
        //cout<<p<<endl;
        for(int j=1;j<=cnt;j++)
        {
            if(p*b[j]>1e5+10)break;
            if(vis[p*b[j]])ans+=vis[p*b[j]];
            //cout<<p*a[j]<<" "<<ans<<" "<<vis[p*b[j]]<<endl;
        }
        vis[a[i]]++;
    }
    printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/liuquanxu/p/11753698.html