More Divisors
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 100
Sample Output:
6 12 60
反素数定义:
对于任何正整数x,其约数的个数记作g(x)。例如g(1)=1、g(4) =3 , g(6)=4。
如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数。例如,整数1,2,4,6等都是反质数。
两个性质:
性质一:一个反素数的质因子必然是从2开始连续的质数.
性质二:p=2^t1*3^t2*5^t3*7^t4.....必然t1>=t2>=t3>=....
题意求出n内约数最多的数,若存在多个则输出最小的那个。
根据性质二暴力dfs搜索出所有的反素数:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
int num;
long long n;
long long ans;
long long k;
//得到的数字 //素数的下标 //约数个数
void dfs(long long sum,int index,int cnt)
{
if(cnt>k)
{
k=cnt;
ans=sum;
}
if(cnt==k&&sum<ans)
{
ans=sum;
}
if(index>14) return ;
if(sum>n) return;
for(int i=1;i<=64;++i)
{
if(sum*p[index] > n) break ;
dfs(sum*=p[index],index+1,cnt*(i+1)) ;
}
}
int main()
{
while(scanf("%lld",&n)==1)
{
k=1;
ans=1;
dfs(1,0,1);
cout<<ans<<endl;
}
return 0;
}