zoukankan      html  css  js  c++  java
  • 牛客——牛牛的最大兴趣组(思维+质因子分解)

    原题链接

    思路:

    参考题解的思路:
    首先,每个整数都可以写成(a*b^{3})的形式;由于问题中求的是立方,将数唯一分解之后,大于(3)的幂次是没有贡献的,所以对质因子的幂次(%3)即可。
    其次,不能共存的数是一一对应的,假设这两个数为(x,y),质因子分解后,对应的幂次为(x(0,1,2)==y(0,2,1)),这样才无法共存。所以只需要将不能共存的数分类,每次取两者之中最多的那个即可。
    最后,考虑如何获得配对的另一半,将(x)变为(x*x)即可,这样幂次分别变成了((0,1+1,(2+2) 取余3 )).

    代码:

    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include<map>
    #include<vector>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b, ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
    const int inf = 0x3f3f3f3f;
    #define PI acos(-1)
    
    const int maxn=1e5+10;
    
    ll n,a[maxn];
    
    ll prime[1300],idx,vis[maxn];
    
    void init(){
        for(ll i=2;i<=1300;i++){
            if(vis[i]) continue;
            prime[++idx]=i*i*i;
            for(ll j=i*i;j<=1300;j+=i)
                vis[j]=1;
        }
    }
    
    map<ll,ll>mp;
    
    ll cul(ll x){
        for(int i=1;i<=idx;i++){
            while(x%prime[i]==0) 
                x/=prime[i];
            if(prime[i]>x) break;
        }
        return x;
    }
    
    int main()
    {
        init();
        n=read;
        rep(i,1,n) a[i]=read;
        rep(i,1,n){
            a[i]=cul(a[i]);
            mp[a[i]]++;
        }
        
        ll res=0;
        
        for(auto t=mp.begin();t!=mp.end();t++){
            if(t->first==1) res++;
            else{
                ll tmp=cul((t->first)*(t->first));
                auto t1=mp.find(tmp);
                if(t1==mp.end()){
                    res+=t->second;
                    t->second=0;
                }
                else{
                    res+=max(t->second,t1->second);
                    t->second=t1->second=0;
                }
            }
        }
        
        printf("%lld
    ",res);
        
        return 0;
    }
    
    /*
    
    **/
    
    
    
    
    
  • 相关阅读:
    数组splice用法
    opacity 适配Ie
    直接贴页面,页面衔接处总会有一像素的间隔
    <script src='url'</script>显示问题
    弹出层
    CF789A. Anastasia and pebbles
    CF789C. Functions again
    HDU2161 Primes
    UVA11752 The Super Powers
    UVA11827 Maximum GCD
  • 原文地址:https://www.cnblogs.com/OvOq/p/14828998.html
Copyright © 2011-2022 走看看