ZOJ_2562_More Divisors(反素数)

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


Author:  Andrew Stankevich
Source:  Andrew Stankevich's Contest #4

题型:数论


题意:输入N,求不超过N的最小的拥有最多因子的数。


分析:

就是求N范围内最大的反素数。


反素数的概念:

       对于任何正整数x起约数的个数记作g(x),例如g(1)=1,g(6)=4。

       定义:如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数。


现在要求N范围内最大的反素数,如:输入1000得到840.


对于求约数的个数

756=2^2*3^3*7^1    约数的个数为 (2+1)*(3+1)*(1+1)=24


反素数有两个特点:

       1、g(x)表示 x含有因子的数目,设 0<i<=x 则g(i)<=x;
        2、2^t1*3^t2^5^t3*7^t4..... 这里有 t1>=t2>=t3>=t4...

可以根据这两个特点进行递归求解


首先做出三条判断,第一是否该值不超过N的大小,第二比较因子的个数,更新最大的,即更新最大的反素数,第三如果因子数相等,则更新较小的。

然后枚举+递归不断尝试更新,使得得到因子数最大的那个。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long bestres,bestsum,n;
long long pri[16]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
//res表示当前讨论的值,sum表示该值的因子数目,
//k表示讨论到第几个素数了,limit表示上次的幂的值
//这次幂的上限,就是体现t1>=t2>=t3...
void work(long long res, long long sum,int k,int limit ){
    if(res>n)//如果超过n就跳出!
        return ;
    if(sum>bestsum){
      //这次计算的因子数目比我以前过程的最大值还大,那么就要更新!
        bestres=res;
        bestsum=sum;
    }
    if(sum==bestsum&&res<bestres){
//如果因子数目一样,取值比较小的!
        bestres=res;
    }
    long long p=pri[k];
//记得第一次p=pri[k],因为下面i是从1开始计数的!
    for(int i=1;i<=limit;i++,p*=pri[k]){
        if(res*p>n)
            break;
        else
            work(res*p,sum*(i+1),k+1,i);
    }
}
int main(){
    while(scanf("%lld",&n)!=EOF){
        bestres=1LL;
        bestsum=1LL;
        work(1LL,1LL,0,50);
        printf("%lld\n",bestres);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值