zoukankan      html  css  js  c++  java
  • 牛客——变换(思维+质数)

    传送门

    思路:

    有个很重要的转化:
    将某个数不变,其他数乘以某个素数
    等价于
    将某个数除以素数,其他数不变

    可以看出两者的作用是相同的。

    所以题意就转化成了:给出一个序列,每次选择一个数除以某个素数,问多少次能够将序列里的所有数变成一样的。

    将所有的数转化成他们的gcd就好了。

    对每个数进行质因子分解,并且记录每个质因子出现的次数。
    对于一个质因子:如果出现的次数大于(n),那么他多出来的部分就要花费代价;
    如果出现的次数小于(n),这些部分都是花费代价。

    代码:

    #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=1e6+10;
    
    ll n,a[maxn];
    
    ll prim[maxn],minn[maxn],cnt[maxn],sum[maxn],idx,vis[maxn];
    vector<int>g[maxn];
    
    void init(){
        for(int i=2;i<maxn;i++){
            if(vis[i]) continue;
            prim[++idx]=i;
            for(int j=2*i;j<maxn;j+=i)
                vis[j]=1;
        }
        for(int i=1;i<=idx;i++){
            for(int j=prim[i];j<maxn;j+=prim[i])
                g[j].push_back(prim[i]);
        }
    }
    
    int main()
    {
        init();
        memset(minn,0x3f,sizeof minn);
        n=read;
        rep(i,1,n) a[i]=read;
        rep(i,1,n){
            if(a[i]==1) continue;
            for(int t:g[a[i]]){
                ll tmp=0;
                while(a[i]%t==0){
                    tmp++;a[i]/=t;
                }
                cnt[t]++;
                sum[t]+=tmp;
                minn[t]=min(minn[t],tmp);
            }
        }
        ll res=0;
        rep(i,1,maxn-1){
            if(cnt[i]==n){
                res+=sum[i]-n*minn[i];
            }
            else res+=sum[i];
        }
        printf("%lld
    ",res);
        return 0;
    }
    
    /*
    
    **/
    
    
    
    
    
    
  • 相关阅读:
    Leetcode Binary Tree Preorder Traversal
    Leetcode Minimum Depth of Binary Tree
    Leetcode 148. Sort List
    Leetcode 61. Rotate List
    Leetcode 86. Partition List
    Leetcode 21. Merge Two Sorted Lists
    Leetcode 143. Reorder List
    J2EE项目应用开发过程中的易错点
    JNDI初认识
    奔腾的代码
  • 原文地址:https://www.cnblogs.com/OvOq/p/14829088.html
Copyright © 2011-2022 走看看