反素数入门 zoj 2562 and codefore

本文介绍了反素数的概念及其在ACM竞赛中的应用,包括如何通过质因数分解来寻找具有特定约数数量的最小整数,并提供了解决这类问题的C++代码示例。

学习资料来源传送们


反素数的定义:对于任何正整数,其约数个数记为,例如,如果某个正整数满足:对任意的正整

            数 ,都有,那么称为反素数


定义理解:素数的约数只有两个 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值