CC

自然万物都趋向从有序变得无序

0%

More Divisors ZOJ - 2562 反素数

题目链接

题目


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


题意:

求小于 n 并且因子个数最多的那个数。

思路:

反素数 (百度百科):对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足, 对于任意的 i (0 < i < x ) 都有 g(i) < g(x) , 则称 x 为反素数。

性质:一个反素数的质因子必然是从2开始连续的质数.
p=2t1*3t2*5t3*7t4…必然t1>=t2>=t3>=…

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

typedef long long LL;
int pri[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61};
LL n, ans, tmp;
const LL INF = 0x3f3f3f3f;

void dfs(int step, LL sum, LL num){//num为因子个数
if(step == 16) return;
if(num > tmp){
ans = sum;
tmp = num;
}if(sum > n) return ;
if(num == tmp && sum < ans)//因子个数一样,选取较小的那个数
ans = sum;
for(int i = 1; i <= 63; ++i){//最多为2^63
if(n / pri[step] < sum) break;
dfs(step + 1, sum *= pri[step], num *(i + 1));
}
}

int main(){
ios::sync_with_stdio(false);
while(cin >> n){
tmp = 0;
ans = INF;
dfs(0, 1, 1);
cout<<ans<<endl;
}return 0;
}