数学(反素数)

反素数

  1. 定义:对于任何正整数n,其约数个数记为f(n),例如f(6)=4;如果存在一个正整数n满足:对于任意的正整数x(0<x<n),都有f(x)<f(n)成立,那么把n称为反素数。

    1. 一个反素数的所有质因子必然是从2开始的若干个质数,因为一个数是反素数,说明在跟它约数相同的数中,它是最小的。
    2. 如果n=2t1 * 3t2 * 5t3 *...,那么一定有t1>=t2>=t3>=t4... 另外易知如果n=2t1* 3t2 * 5t3 * ...,那么n的约数个数为(t1+1) *(t2+1) * (t3+1)...

先贴个板子:

void dfs(ll num,int k,int sum,int limit)
{//num: 当前枚举到的数 k:枚举到的第k大的质因子 sum:该数的约数个数 limit:质因子个数上限(重要剪枝)
    if(sum>maxsum)       //约数个数更多
    {
        maxsum=sum;
        ans=num;
    }
    if(sum==maxsum&&ans>num)  //约数个数相同,把最优解更新为较小值
    {
        ans=num;
    }
    if(k>16)                     //这里k>x; x至少满足prime[1]*prime[2]*prime[3]*...*prime[x]>x ,当x=16时,数据已超过10^18
        return;                  //当x=10时,数据已超过10^9
    ll temp=num;
    for(int i=1;i<=limit;i++)  //枚举每个质因子的个数
    {
        if(n/prime[k]<temp)    //n为上限,用除法防止溢出
            return;
        temp*=prime[k];
        dfs(temp,k+1,sum*(i+1),i);  //把limit置为i的原因见性质第二条
    }
}

再来个例题:

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

板子题就直接贴代码了

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll INF=0x3f3f3f;
ll ans,maxsum=0;
ll n;
int prime[10001];//={1,2,3,5,7,9,11,13,17,19,23,29,31,37,41,43,47,53};
void set_prime()
{
    bool vis[10000];
    prime[0]=1;
    int cnt=0;
    memset(vis,0,sizeof vis);
    for(int i=2;i<10000;i++)
        if(!vis[i])
    {
        prime[++cnt]=i;
        for(int j=1;i*j<10000;j++)
            vis[i*j]=1;
    }
}
void dfs(ll num,int k,int sum,int limit)
{
    if(sum>maxsum)
    {
        maxsum=sum;
        ans=num;
    }
    if(sum==maxsum&&ans>num)
        ans=num;
    if(k>16)
        return;
    ll temp=num;
    for(int i=1;i<=limit;i++)
    {
        if(n/prime[k]<temp)
            return;
        temp*=prime[k];
        dfs(temp,k+1,sum*(i+1),i);
    }
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    set_prime();
    while(cin>>n)
    {
        ans=INF*INF;
        maxsum=0;
        dfs(1,1,1,54);
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shaohang_/article/details/81463467