Given an integer n(n<=10^16), 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.
样例:100 结果60
分析:
1、n = p1^n1 * p2^n2 * p3^n3 * ... * pk^nk
MaxDivisors = (n1+1)*(n2+1)*(n3+1)* ... * (nk+1); 2.在一个数范围之内,相对于因子个数最多的数来说,p越小,n越大才是最优的,同时可以优化剪枝 若p1<p2,则n1>=n2;
#include<stdio.h>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
int prime[41]={2,3,5,7,11,13,17,19,23,29,31,37,41,43};//这些素因子之积会超过n的范围
ll Maxdivisors;
ll result;
ll n;
ll Pow(int x,int k){
ll s=1;
for(int i=1;i<=k;i++)
{
s*=x;
}
return s;
}
void dfs(int x,ll now,ll divisors,int lastNi){
if(Maxdivisors<divisors||(Maxdivisors==divisors&&result>now)){
Maxdivisors=divisors;
result=now;
}
int i=1;
while(now*Pow(prime[x],i)<n&&i<=lastNi){
dfs(x+1,now*Pow(prime[x],i),divisors*(i+1),i);
i++;
}
}
int main(){
while(scanf("%lld",&n)!=EOF){
int a=log(n);
Maxdivisors=1;
result=n;
for(int i=1;i<=a;i++){ //枚举初始值素因子2的个数
dfs(1,Pow(2,i),i+1,i);
}
printf("%lld\n",result);
}
return 0;
}