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.
题意:给出一个数n求小于等于n的数中因子数最多的一个数,且此数最小;
求一个数的因子数就是分解质因数呗,但是n的范围太大了肯定超时,
既要数小又要因子数多那就素因子越少幂次越高越好即
p=2^t1*3^t2*5^t3···t1>=t2>=t3>=···
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<iomanip>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
using namespace std;
typedef long long ll;
int prim[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
ll maxn,best,n;
void dfs(ll sum,ll cur,int k,int t)
{
//sum当前约数的总个数,cur当前数值及所有素因子的乘积,k正在使用的素数,t当前素数可达的最高次幂
if(maxn<sum)
{
maxn=sum;
best=cur;
}
if(sum==maxn&&cur<best)
{
best=cur;
}
if(k>14)
return ;
ll tmp=cur;
for(int i=1;i<=t;i++)
{
if(tmp*prim[k]>n)
break;
tmp*=prim[k];
dfs(sum*(i+1),tmp,k+1,i);
}
}
int main()
{
while(~scanf("%lld",&n))
{
maxn=best=1;
dfs(1,1,0,50);
printf("%lld\n",best);
}
}