UVA294

在这里插入图片描述

题目要求指定范围内约数数目最多的数以及对应的约数数量。
根据唯一分解定理,对于任意正整数$2\leq N$,有:

那么根据乘法原理,约数的总数目为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

const int MAXN = 1000001;

int main() {
int T;
cin >> T;
int Left, Right;
while (T--) {
cin >> Left >> Right;
int Ans = 0, Node = Left;
for (int n = Left; n <= Right; ++n) {
int Number = n;
int Count = 1;
int Max = ceil(sqrt(Right));
int NumberOfDifP = 0;
for (int i = 2; i <= Max; ++i) {
if (Number % i == 0) {
++NumberOfDifP;
int temp = 0;
while (Number % i == 0) {
Number /= i;
++temp;
}
Count *= (temp + 1);
}
}
if (NumberOfDifP == 0) {
Count = 1;
}
if (Number != 1) {
++Count;
}
if (Ans < Count) {
Ans = Count;
Node = n;
}
}
printf("Between %d and %d, %d has a maximum of %d divisors.\n", Left, Right, Node, Ans);
}

return 0;

}