zoukankan      html  css  js  c++  java
  • 19年徐州E题--大数随机质因数分解

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    const double eps = 1e-8;//精度
    const double inf = 1e20;
    const double pi = acos(-1.0);
    const int mod = 998244353;
    const int N = 1e5 + 10;
    ll ct,cnt;
    const int Times = 10;
    ll fac[N],num[N];
    ll a[N],times[N];
    __int128 temp2[N];
    ll gcd(ll a,ll b)
    {
        return b?gcd(b,a%b):a;
    }
    ll multi(ll a,ll b,ll m)
    {
        ll ans = 0;
        a %= m;
        while(b)
        {
            if(b&1)
            {
                ans = (ans + a) % m;
                b--;
            }
            b >>= 1;
            a = (a + a) % m;
        }
        return ans;
    }
    ll quick_mod(ll a,ll b,ll m)
    {
        ll ans = 1;
        a %= m;
        while(b)
        {
            if(b&1)
            {
                ans = multi(ans,a,m);
                b--;
            }
            b>>=1;
            a = multi(a,a,m);
        }
        return ans;
    }
    bool Miller_Rabin(ll n)
    {
        if(n == 2) return true;
        if(n < 2 || !(n&1)) return false;
        ll m = n - 1;
        int k = 0;
        while((m & 1) == 0)
        {
            k++;
            m >>= 1;
        }
        for(int i=0;i<Times;i++)
        {
            ll a = rand() % (n-1) + 1;
            ll x = quick_mod(a,m,n);
            ll y = 0;
            for(int j=0;j<k;j++)
            {
                y = multi(x,x,n);
                if(y == 1 && x != 1 && x != n-1)
                    return false;
                x = y;
            }
            if(y!=1)
                return false;
        }
        return true;
    }
    ll pollard_rho(ll n,ll c)
    {
        ll i = 1,k = 2;
        ll x = rand() % (n-1) + 1;
        ll y = x;
        while(true)
        {
            i++;
            x = (multi(x,x,n)+c) % n;
            ll d = gcd((y-x+n)%n,n);
            if(1<d && d < n) return d;
            if(y == x) return n;
            if(i == k)
            {
                y = x;
                k <<= 1;
            }
        }
    }
    void find(ll n,ll c)
    {
        if(n == 1) return;
        if(Miller_Rabin(n))
        {
            fac[ct++] = n;
            return;
        }
        ll p = n;
        ll k = c;
        while(p >= n)
            p = pollard_rho(p,c--);
        find(p,k);
        find(n/p,k);
    }
    void fenjie(ll n)
    {
        ct = 0;
        find(n,1111);
        sort(fac,fac+ct);
    
        num[0] = 1;
        int k = 1;
        for(int i=1;i<ct;i++)
        {
            if(fac[i] == fac[i-1])
            {
                ++num[k-1];
            }
            else
            {
                num[k] = 1;
                fac[k++] = fac[i];
            }
        }
        cnt = k;
    //    for(int i=0;i<cnt;i++)
    //    {
    //        cout << fac[i] << " " << num[i] << " ";
    //    }
    //    cout << "
    ";
    }
    void sum(ll y,ll flag)
    {
        for(int i=0;i<cnt;i++)
        {
            temp2[i] = fac[i];
        }
        for(int i=0;i<cnt;i++)
        {
            while(temp2[i] - y <= 0)
            {
                times[i] += flag * y / temp2[i];
                temp2[i] = temp2[i] * fac[i];
            }
        }
    }
    void solve()
    {
        ll n,x,y;
        cin >> n >> x >> y;
        for(int i=1;i<=n;i++)
        {
            cin >> a[i];
        }
        fenjie(x);
        for(int i=0;i<cnt;i++)
        {
            //初始化
            times[i] = 0;//统计y!在fac[i]^num[i]对应的因子
        }
        sum(y,1);
        ll ans = 4e18;
        for(int i=1;i<=n;i++)
        {
            sum(a[i],-1);
        }
        for(int i=0;i<cnt;i++)
        {
            ans = min(ans,times[i]/num[i]);
        }
        cout << ans << "
    ";
    
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie();
        cout.tie();
        int T;
        cin >> T;
        while(T--)
        {
            solve();
        }
    }
    /*
    1
    1 9 15
    1
     */
    
  • 相关阅读:
    Keepalived安装使用详解
    Django缓存使用方法
    Django常用命令及参数配置(Django 1.8.6)
    Python之Rpyc模块
    Hadoop:实战Web日志分析
    Hadoop:使用Mrjob框架编写MapReduce
    Hadoop:使用原生python编写MapReduce
    Hadoop安装
    LGOJP1850 换教室
    BZOJ4318: OSU!
  • 原文地址:https://www.cnblogs.com/hh13579/p/14076513.html
Copyright © 2011-2022 走看看