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
题意:给出一个数n,求n以内的一个因子个数最多的且大小最小的数。
思路:
对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.
如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数.
题目转化为给一个N,求出不超过N的最大的反素数.
比如:输入1000 输出 840
求[1..N]中约数在大的反素数-->求约数最多的数
如果求约数的个数 756=2^2*3^3*7^1
(2+1)*(3+1)*(1+1)=24
基于上述结论,给出算法:按照质因数大小递增顺序搜索每一个质因子,枚举每一个质因子
为了剪枝:
性质一:一个反素数的质因子必然是从2开始连续的质数.
10^16以内的数最多只需要前16个素数构造
性质二:p=2^t1*3^t2*5^t3*7^t4.....必然t1>=t2>=t3>=....
AC代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#define ll long long
using namespace std;
const int prime[16] = {1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
ll n, minnum, maxsum;
void dfs(ll num, ll sum, ll k, int cnt)
{
if(sum > maxsum){
minnum = num;
maxsum = sum;
}
else if(sum == maxsum && num < minnum) minnum = num;
if(k > 15) return;
ll tmp = num;
for(int i = 1; i <= cnt; i++)
{
tmp *= prime[k];
if(tmp > n) break;
dfs(tmp, sum * (i + 1), k + 1, i);
}
}
int main()
{
while(~scanf("%lld", &n))
{
minnum = n;
maxsum = 0;
dfs(1, 1, 1, 50);
printf("%lld\n", minnum);
}
return 0;
}