您的位置:首页 > 其它

反素数入门 zoj 2562 and codefore

2017-11-20 16:52 225 查看
学习资料来源传送们

反素数的定义:对于任何正整数

,其约数个数记为

,例如

,如果某个正整数

满足:对任意的正整

            数

,都有

,那么称

为反素数

定义理解:素数的约数只有两个 1  和  本身,而反素数的约数是尽可能多,比这个数小到正数的约数都要多

在ACM竞赛中,最常见的问题如下:

(1)给定一个数

,求一个最小的正整数

,使得

的约数个数为


(2)求出

中约数个数最多的这个数

题目:

http://codeforces.com/problemset/problem/27/E

题意:

给出一个数 n  ,求一个最小的数,这个数的约数个数恰好为 n 个

题解:

对于一颗质因子分解的树进行搜索

例如:


,以每一个

为树的一层建立搜索树,深度为




为例进行说明,建树如下:

 



/*************************************************************************
> File Name: main.cpp
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: 2017年11月21日 星期二 00时24分07秒
************************************************************************/

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

#define ULL unsigned long long
#define INF ~0ULL
int n;
ULL ans;
int p[16]={
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53
};

void dfs(int dept,ULL temp,int num){
if(num>n) return ;
if(num==n&&ans>temp) ans=temp;
for(int i=1;i<64;i++){
if(ans/p[dept]<temp) break;
dfs(dept+1,temp*=p[dept],num*(i+1));
}
}

int main()
{
freopen("in.txt","r",stdin);
while(cin>>n){
ans=INF;
dfs(0,1,1);
cout<<ans<<endl;
}
return 0;
}
 


zoj2562
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1562
题意:

给出n,求1~n中约数个数最多的数字

题解:

修改上叙代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

#define ULL unsigned long long
#define INF ~0ULL
int best;
ULL n,ans;
int p[16]={
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53
};

void dfs(int dept,ULL temp,int num){
if(dept>=16) return;
if(num>best){
best=num;
ans=temp;
}
if(num==best&&ans>temp) ans=temp;
for(int i=1;i<64;i++){
if(n/p[dept]<temp) break;
dfs(dept+1,temp*=p[dept],num*(i+1));
}
}

int main()
{
freopen("in.txt","r",stdin);
while(cin>>n){
ans=INF;
best=0;
dfs(0,1,1);
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: