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;
}

 






 

转载于:https://www.cnblogs.com/Patt/p/5296341.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值