[BZOJ1053] 反素数 题解

题目描述

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

如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数。例如,整数1,2,4,6等都是反质数。

现在给定一个数N,你能求出不超过N的最大的反质数么?

输入输出格式

输入格式:
一个数N(1<=N<=2,000,000,000)。

输出格式:
不超过N的最大的反质数。

输入输出样例

输入样例#1:
1000
输出样例#1:
840

思路:发现任一反素数满足质因子为连续素数且指数单调递减,故用dfs寻找其指数.由2 * 3 * 5 * ...* 29 * 31 > INT_MAX可知质因子最多可达31,利用算数基本定理的推论:** 设n = p1 ^ c1 + p2 ^ c2 + ..... + pn ^ cn,则n的质因子个数为(c1 + 1)(c2 + 1)....(cn + 1) ** 在dfs中递归求当前质因子个数即可求解本题.


#include <algorithm>
#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define rg register
#define LL long long
#define __space putchar(' ')
#define __endl putchar('
')
template <typename qwq> inline void read(qwq & x)
{
	x = 0;
	rg int f = 1;
	rg char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	x *= f;
}
template <typename qaq> inline void print(qaq x)
{
	if (x < 0)
	{
		putchar('-');
		x = -x;
	}
	if (x > 9) print(x / 10);
	putchar(x % 10 + '0');
}
inline LL Pow(LL x,LL y)
{
	rg int ret = 1;
	while (y)
	{
		if (y & 1) ret *= x;
		x *= x;
		y >>= 1;
	}
	return ret;
}
int dx[] = {0,2,3,5,7,11,13,17,19,23,29};
LL n,ans,maxx;
inline void dfs(LL data,LL last,LL now,LL num,LL k)
//data:dx[i]的i,last:dx[data - 1]的指数 now:dx[data]的指数,num:当前数字大小,k:当前约数个数 
{
	if (k >= maxx)
	{
		if (k == maxx) ans = min(ans,num);
		else ans = num,maxx = k;
	}
	if (now < last && num * dx[data] <= n) dfs(data,last,now + 1,num * dx[data],k / (now + 1) * (now + 2));
	if (now && num * dx[data + 1] <= n) dfs(data + 1,now,1,num * dx[data + 1],k * 2);
}
int main()
{
	read(n);
	if (n == 1)
	{
		print(1);
		return 0;
	}
	dfs(1,100,0,1,1);
	print(ans);
	return 0;
}
原文地址:https://www.cnblogs.com/Here-is-SG/p/10628914.html