传送门1:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1562
传送门2:http://www.lydsy.com/JudgeOnline/problem.php?id=1053
ZOJ1562题意:求n以内的因子最多的那个数
BZOJ1053题意:不超过n的最大反素数(这题数据稍小)
反素数讲解:点击打开链接
代码:
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const ull inf = ~0ULL;
int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};//这16个素数乘起来接近于ull最大值
//因为质因数的指数不递减,所以所以指数为1的时候不爆ull的最多16个质数
ull ans, n;
int mx;//最多的因数
void dfs(int dep, ull tmp, int num){
//深度,当前数的值,因数的个数
if(dep >= 16)return ;//到叶子结点,返回
//num记录的因子个数,如果遇到更大的,就更新
if(num > mx) ans = tmp, mx = num;
//贪心,当因子个数相同时,取值最小的
if(num == mx && ans > tmp) ans = tmp;
for(int i = 1; i <= 63; i++){ //枚举指数
if(tmp > n / p[dep])break; //超过n就结束
dfs(dep + 1, tmp *= p[dep], num * (i + 1));
}
}
int main(){
while(cin>>n){
ans = inf;
mx = 0;
dfs(0, 1, 1);
cout << ans << endl;
}
return 0;
}