• Codeforces Round #596 div2 D. Power Products


    (预先枚举1e10以内所有数的k次方,然后每一个ai都去找他所有的1e5以内的倍数,更新答案。)
    (复杂度sqrt[k]{1e10} imes{sqrt{1e5} imes{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
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    洛谷 1736 创意吃鱼法
    有多重限制的背包
    洛谷 1417 烹调方案
    2008 noip 传纸条
    环形石子合并 洛谷 1880 && hdu 3506 Monkey Party
    洛谷 1282 多米诺骨牌
    (金明的预算方案)依赖性的背包
    分组背包问题
    混合背包问题
    多重背包问题
  • 原文地址:https://www.cnblogs.com/liuquanxu/p/11753698.html
Copyright © 2020-2023  润新知