Mathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be an interesting number, since it is the first odd number for which the sum of its divisors is larger than the number itself.
To help them search for interesting numbers, you are to write a program that scans a range of numbers and determines the number that has the largest number of divisors in the range. Unfortunately, the size of the numbers, and the size of the range is such that a too simple-minded approach may take too much time to run. So make sure that your algorithm is clever enough to cope with the largest possible range in just a few seconds.
Input
The first line of input specifies the number N of ranges, and each of the N following lines contains a range, consisting of a lower bound L and an upper bound U, where L and U are included in the range. L and U are chosen such that 1 ≤ L ≤ U ≤ 1000000000 and 0 ≤ U − L ≤ 10000.
Output
For each range, find the number P which has the largest number of divisors (if several numbers tie for first place, select the lowest), and the number of positive divisors D of P (where P is included as a divisor). Print the text ‘Between L and H, P has a maximum of D divisors.’, where L, H, P, and D are the numbers as defined above.
Sample Input
3
1 10
1000 1000
999999900 1000000000
Sample Output
Between 1 and 10, 6 has a maximum of 4 divisors.
Between 1000 and 1000, 1000 has a maximum of 16 divisors.
Between 999999900 and 1000000000, 999999924 has a maximum of 192 divisors.
Regionals 1994 >> Europe - Southwestern
问题链接:UVA294 UVALive5595 Divisors
问题简述:(略)
问题分析:
这个题是计算数的正因子数并且求最值,是一个典型的数论题。
首先要求出各个素数因子的次方数,然后算其组合数。
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* UVA294 UVALive5595 Divisors */
#include <bits/stdc++.h>
using namespace std;
int countFactor(int n)
{
int count = 1;
int m;
m = sqrt(n + 0.5);
for (int i = 2; i <= m; i++) {
int c = 0;
while (n % i == 0) {
n /= i;
c++;
}
count *= (c + 1);
}
if (n > 1)
count *= 2;
return count;
}
int main()
{
int n, l, u;
scanf("%d", &n);
while(n--) {
scanf("%d%d", &l, &u);
int ans = 0, num;
for(int i=l; i<=u; i++) {
int tmp = countFactor(i);
if(tmp > ans) {
ans = tmp;
num = i;
}
}
printf("Between %d and %d, %d has a maximum of %d divisors.\n", l, u, num, ans);
}
return 0;
}