Everybody knows that we use decimal notation, i.e. the base of our notation is 10. Historians say that it is so because men have ten fingers. Maybe they are right. However, this is often not very convenient, ten has only four divisors -- 1, 2, 5 and 10. Thus, fractions like 1/3, 1/4 or 1/6 have inconvenient decimal representation. In this sense the notation with base 12, 24, or even 60 would be much more convenient.
The main reason for it is that the number of divisors of these numbers is much greater -- 6, 8 and 12 respectively. A good quiestion is: what is the number not exceeding n that has the greatest possible number of divisors? This is the question you have to answer.
Input:
The input consists of several test cases, each test case contains a integer n (1 <= n <= 1016).
Output:
For each test case, output positive integer number that does not exceed n and has the greatest possible number of divisors in a line. If there are several such numbers, output the smallest one.
Sample Input:10 20 100Sample Output:
6 12 60
Author: Andrew Stankevich
Source: Andrew Stankevich's Contest #4
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
const int INF =0x3f3f3f3f;
//const int maxn= ;
ull n;
int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};//
ull ans,best;
void update(ull val,ull num)//更新最优值
{
if(ans<num)
{
ans=num;
best=val;
}
else if(ans==num&&val<best)
{
best=val;
}
}
void dfs(int depth,ull val,ull num ,int last )//不用担心层数会超过15,因为从p[0]乘到p[15]接近 10^20。
{
if(val>n) return;
update(val,num);
for(int i=1;i<=min(63,last);i++)//根据反素数的特点,小的素数的次数>=大的素数的次数。
{
if( n<val*p[depth]) break;//如果超过范围了,剪掉
dfs(depth+1,val*=p[depth] ,num*(i+1),i );
}
}
//dfs函数实际时间复杂度非常低,因为假如从0到15每一层i都选择1,那么乘积已经接近10^20了。
//不能有中间层选择0,而后面选择>0次。
//因为根据反素数的特点,小的素数的次数>=大的素数的次数。
int main()
{
while(cin>>n)
{
ans=1,best=1;//如果定义了ull,千万别再赋值为负数了。
dfs( 0,1 , 1 ,INF);
cout<<best<<endl;
}
return 0;
}