【BZOJ1053】[HAOI2007]反素数

【BZOJ1053】[HAOI2007]反素数

题面

bzoj

洛谷

题解

可以从反素数的定义看出小于等于\(x\)的最大反素数一定是约数个数最多且最小的那个

可以枚举所有的质因数来求反素数,但还是跑不过

我们又想,质因数不可能太大

\(37\)内素数相乘已经大于\(2*10^9\)

所以枚举到\(37\)就可以了

代码

#include <iostream> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <cmath> 
#include <algorithm> 
using namespace std; 
int N; 
int prime[12] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
int ans = 2e9 + 1, tot; 
void dfs(int x, int sum, int num) { 
	if (x == 12) { 
		if (num > tot) ans = sum, tot = num;
		else if (num == tot) ans = min(ans, sum);
		return ; 
	} else {
		long long res = 1, cnt = 1;
		while (1) { 
			if (1ll * sum * res > 1ll * N) break; 
			dfs(x + 1, sum * res, num * cnt); 
			++cnt; res *= prime[x];
		} 
	} 
} 
int main () {
	cin >> N; 
	dfs(0, 1, 1);
	cout << ans << endl; 
	return 0; 
} 
posted @ 2018-12-26 15:22  heyujun  阅读(241)  评论(0编辑  收藏  举报