码迷,mamicode.com
首页 > 其他好文 > 详细

zoj2562--More Divisors(反素数模板)

时间:2014-09-16 22:10:21      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   strong   for   


More Divisors

 

Time Limit: 2 Seconds      Memory Limit: 65536 KB

 

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

从反素数的定义中可以看出两个性质:

 

(1)一个反素数的所有质因子必然是从2开始的连续若干个质数,因为反素数是保证约数个数为bubuko.com,布布扣的这个数bubuko.com,布布扣尽量小

(2)同样的道理,如果bubuko.com,布布扣,那么必有bubuko.com,布布扣


 

#include <cstdio>
#define LL long long
LL maxsum , bestnum , n ;
LL pri[20]={2,3,5,7,11,13,17,19,23,29,31,37,39,41,43,47,53};  

void f(LL num,LL k,LL sum,LL limit)
{
    LL i , temp ;
    if( sum > maxsum )
    {
        maxsum = sum ;
        bestnum = num ;
    }
    if( sum == maxsum && bestnum > num )
        bestnum = num ;
    if(k == 20) return ;
    temp = num ;
    for(i = 1 ; i <= limit ; i++)
    {
        if( temp*pri[k] > n )
            break;
        temp = temp*pri[k] ;
        f(temp,k+1,sum*(i+1),i);
    }
    return ;
}
int main()
{
    while(scanf("%lld", &n)!=EOF)
    {
        bestnum = maxsum = -1 ;
        f(1,0,1,50);
        printf("%lld\n", bestnum);
    }
}


 

 

 

 

zoj2562--More Divisors(反素数模板)

标签:style   blog   http   color   io   os   ar   strong   for   

原文地址:http://blog.csdn.net/winddreams/article/details/39323479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!