ZOJ 2562 More Divisors 解题报告(反素数)

96 篇文章 0 订阅
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

    解题报告: 首先看题意,让我们求的是小于等于n的约数最多的最小的数。
    我们可以将一个整数表示成  n = p1^e1*p2^e2*...*pt*et,那么约数的个数则是(e1+1)*(e2+1)*...*(et+1)。
    约数最多,且数最小,那么必然要满足e1>=e2>=...>=et。否则就有一个约数相同,且比它更小的数(将ei从小到大排序)。
    因为pi的大小与约数的多少无关,那么pi越小越好,也就是2,3,5,7,11,13。。。
    枚举这些数的幂,求出最大约数对应的数即可。
    可以在百度百科上查询"反素数",上面也有代码。
    代码如下:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

typedef long long LL;
LL sum, maxSum;
LL minNum, n;

int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};

void getAntiPrime(LL num, LL sum, int k, int limit)
{
    if(sum>maxSum)
    {
        maxSum=sum;
        minNum=num;
    }
    else if(sum==maxSum && num<minNum)
    {
        minNum=num;
    }
    else if(num>=minNum)
    {
        return;
    }

    if(k==15) return;

    for(int i=1;;i++)
    {
        num*=prime[k];
        if(num>n) break;
        getAntiPrime(num, sum*(i+1), k+1, i);
    }
}

int main()
{
    while(~scanf("%lld", &n))
    {
        maxSum=0;
        getAntiPrime(1,1,0,50);
        printf("%lld\n", minNum);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值