LuoguP1069 细胞分裂 (质因数分解)

通过质因数分解可以把很大的数唯一分解为有限个质数的乘积,能够很好的比较两个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
#define rep(i, a, n) for(int i = a; i <= n; ++ i)
#define per(i, a, n) for(int i = n; i >= a; -- i)
typedef long long ll;
const int N = 2e5+105;
const int mod = 998244353;
const double Pi = acos(- 1.0);
const ll INF = 1e12;
const int G = 3, Gi = 332748118;
ll qpow(ll a, ll b) { ll res = 1; while(b){ if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1;} return res; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
//



ll n, m1, m2;
ll p[N], c[N], m, res;
map<ll, ll> mp;
vector<ll> base;


void div(ll x){
    m = 0;
    for(ll i = 2; i * i <= x; ++ i){
        if(x % i == 0){
            p[++ m] = i;
            c[m] = 0;
            while(x % i == 0) c[m] ++, x /= i;
        }
    }
    if(x > 1) p[++ m] = x, c[m] = 1;
}

ll solve(ll x){
    div(x);
   
    if(m < (ll)base.size()) return INF;
    ll l = 1, r = 0, num = 0, temp = 0;
    while(r < base.size()){
        num = 0;
        ll pp = base[r], cc = mp[base[r]];
        if(p[l] == pp){
            if(cc <= c[l]) num = 0;
            else num = (cc + c[l] - 1) / c[l];
            temp = max(temp, num);
            l ++;
            r ++;
        }
        else if(p[l] < pp){
            l ++;
            if(l > m){
                return INF;
            }
        }
        else{
           l ++;
           if(l > m){
               return INF;
           }           
        }
    }
    return temp;
}

int main()
{
    scanf("%lld",&n);
    scanf("%lld%lld",&m1,&m2);
    if(m1 == 1){
        printf("0\n");
        return 0;
    }
    div(m1);
    rep(i,1,m){
        mp[p[i]] = m2 * c[i];
        base.push_back(p[i]);
    }
    sort(base.begin(),base.end());
    res = INF;
    rep(i,1,n){
        ll s;
        scanf("%lld",&s);
        res = min(res, solve(s));
    }
    if(res == INF) printf("-1\n");
      else printf("%lld\n",res);
    return 0;
}
posted @ 2020-05-03 16:34  A_sc  阅读(146)  评论(0编辑  收藏  举报