zoukankan      html  css  js  c++  java
  • hihocoder1187 Divisors

    传送门

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    Given an integer n, for all integers not larger than n, find the integer with the most divisors. If there is more than one integer with the same number of divisors, print the minimum one.

    输入

    One line with an integer n.

    For 30% of the data, n ≤ 103

    For 100% of the data, n ≤ 1016

    输出

    One line with an integer that is the answer.

    样例输入
    100
    样例输出
    60
    _______________________________________
    Solution:
    将正整数n进行质因数分解:
      n = p1^k1 * p2^k2 * ... * pm^km
      (p1 < p2 < ... < pm)
    易见,n的因子数为
      (1+k1)*(1+k2)*...*(1+km)
    接下来考虑不超过N的正整数中,因子数最多且最小的数,记为s(N)。
    我们可以推导出s(N)的质因数分解的形式有下列特征:
    (1)  s(N) = 2^k1 * 3^k2 * 5^k3 * ...* p[i]^ki * ... *p[m]^km
    式中p[i]表示第i个质数。
    (2)  k1 <= K2 <= k3 <= ... <=km
    可概括为:
    (1)质因子连续
    (2)指数单调不增
    因此可采取暴力搜索(DFS)的办法求解。
    ————————————————————————————————————
    Complexity:
    前14个质数的乘积为 13082761331670030 ~ 1.3E16
    所以搜索层数最多为13层,复杂度可大胆估计为2^13,这里不需要估计得比较准确。
    ————————————————————————————————————————————————————————————
    Implementation:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    
    const int N(105);
    bool p[N];
    int P[N];
    int np;
    
    void seive(int n){
        memset(p, 1, sizeof(p));
        p[0]=p[1]=0;
        for(int i=2; i<=n; i++){
            if(p[i]) P[np++]=i;
            for(int j=i<<1; j<=n; j+=i)
                p[j]=0;
        }
    }
    
    LL ans, val, n;
    
    
    void dfs(int lev, LL res, int cnt, LL v){
        if(v*P[lev]>n){
            if(res>ans){
                ans=res;
                val=v;
            }
            else if(res==ans&&val>v)
                val=v;
        }
        else{
            for(int i=1; v*P[lev]<=n&&i<=cnt; i++){
                v*=P[lev];
                dfs(lev+1, res*(i+1), i, v);
            }
        }
    }
    
    int main(){
        seive(100);
        cin>>n;
        val=n;
        dfs(0, 1, 100, 1);
        cout<<val<<endl;
    }





  • 相关阅读:
    过滤器,拦截器,监听器的区别
    RedisTemplate常用集合使用说明-opsForZSet(六)
    RedisTemplate常用集合使用说明-opsForSet(五)
    RedisTemplate常用集合使用说明-opsForHash(四)
    RedisTemplate常用集合使用说明-opsForList(三)
    pip 加速方案
    swoole 使用 1
    Fatal error in launcher: Unable to create process using '"'
    webpack 的简单使用
    我 && symfony3 (路由)
  • 原文地址:https://www.cnblogs.com/Patt/p/5296341.html
Copyright © 2011-2022 走看看