p1186反素数(模板题)

    

描述 Description
如果一个自然数比所有比它小的自然数的约数个数都要多,那么我们就称这个数为一个反素数。例如,1、2、4、6、12和24都是反素数。
任务:
请写一个程序:
○ 读入一个自然数n;
○ 找出不大于n的最大的反素数;
○ 将结果输出。


输入格式 Input Format
只包含一行,为一个自然数n,1<n<2000000000。


输出格式 Output Format
输出唯一的一个整数——不大于n的最大反素数


样例输入 Sample Input
1000


样例输出 Sample Output
840


时间限制 Time Limitation
1s


注释 Hint
1s


来源 Source
noi导刊2011暑期培训

 

      通过这个题了解了一下反素数,定义:对于任何正整数x,其约数的个数记作g(x)。例如g(1)=1、g(6)=4。
       如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数。所以,n以内的反质数即为不超过n的约数个数最多的数。

     对于一个大于1正整数n可以分解质因数:n=p1^a1*p2^a2*p3^a3*…*pk^ak,则n的正约数的个数就是(a1+1)(a2+1)(a3+1)…(ak+1) .

     其中a1、a2、a3…ak是p1、p2、p3,…pk的指数。

     所以从小到大枚举质因数,因为小的质因数肯定比大的质数高

     代码如下:

      

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
LL yi[15]={2,3,5,7,11,13,17,19,23,29};
LL n;
LL big,num;
void dfs(LL x,int y,int limit,int z)
{
    if(x>n)//如果这个数大于n的话退出
        return;
    if(num<y)//如果当前最大数的因数个数小于这个,替换
    {
        big=x;
        num=y;
    }
    if(num==y&&big>x)//如果因数相同,则选小的
    {
        big=x;
    }
    LL temp=x;
    for(int i=1;i<=limit;i++)
    {
        temp=temp*yi[z];
        if(temp>n)
            return;
        dfs(temp,y*(i+1),i,z+1);
    }
}
int main()
{
    cin>>n;
    big=0;
    num=0;
    dfs(1,1,50,0);
    cout<<big<<endl;
    return 0;
}
模板

 

 

 

    

posted @ 2017-08-20 18:00  列車員lcy  阅读(271)  评论(0编辑  收藏  举报