题目链接:
HDU 2521 反素数
题意:
求出区间[a,b]约数最多的数。
分析:
先求
[1,b]
中约数最多的数
ans
,如果
ans∉[a,b]
那就暴力跑。本身这题数据也不大。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long lint;
const lint inf = ~0ull;
const int prime[]= {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
int T, cnt;
int a, b;
lint ans;
void dfs(int depth, int limit, lint tmp, int num)
{
if(tmp > b) return;
if(num > cnt || (num == cnt && tmp < ans)) {
ans = tmp;
cnt = num;
}
for(int i = 1; i <= limit; ++i) {
if((double)tmp * prime[depth] > b) break;
tmp *= prime[depth];
dfs(depth + 1, i, tmp, num * (i + 1));
}
}
int main()
{
scanf("%d", &T);
while(T--) {
cin >> a >> b;
ans = inf, cnt = 0;
dfs(0, 63, 1, 1);
if(ans >= a && ans <= b) cout << ans << endl;
else {
cnt = 0;
for(int i = a; i <= b; ++i) {
int num = 0;
for(int j = 1; j * j <= i; ++j) {
if(i % j == 0) {
if(i / j != j) num += 2;
else num++;
}
}
if(num > cnt) {
cnt = num;
ans = i;
}
}
cout << ans << endl;
}
}
return 0;
}