Divisors
Divisors |
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 Specification
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 and
.
Output Specification
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.', whereL, H, P, and D are the numbers as defined above.
Example input
3 1 10 1000 1000 999999900 1000000000
Example 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.
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#define ONLINE_JUDGE
const int NMAX = 32000;
bool is_prime[NMAX];
int prime[NMAX];
int primes;
void generate_primes()
{
memset (is_prime, 1, sizeof (is_prime));
for (int i=2; i<NMAX; ++i) {
if (! is_prime[i]) {
continue;
}
for (int k=i+i; k<NMAX; k+=i) {
is_prime[k] = false;
}
}
primes = 0;
for (int i=2; i<NMAX; ++i) {
if (is_prime[i]) {
prime[primes++] = i;
}
}
}
int main()
{
generate_primes();
int n = 0;
scanf ("%d", &n);
while (--n >= 0) {
const int RANGE = 10010;
int factors[RANGE];
int L = 0, U = 0;
scanf ("%d%d", &L, &U);
assert (U - L + 1 < RANGE);
for (int i=L; i<=U; ++i) {
static int power[NMAX];
int the_num = i;
int p = 0;
power[0] = 0;
int f = 1;
while (the_num > 1) {
#ifndef ONLINE_JUDGE
printf ("the_num = %d, prime[%d] = %d\n", the_num, p, prime[p]);
#endif
if (the_num % prime[p] == 0) {
the_num /= prime[p];
++power[p];
} else if (p + 1 >= primes) {
f *= 2;
break;
} else {
assert (p + 1 < primes);
power[++p] = 0;
assert (prime[p] <= the_num);
}
}
#ifndef ONLINE_JUDGE
printf ("p = %d\n", p);
#endif
factors[i - L] = 1 * f;
for (int k=0; k<=p; ++k) {
factors[i - L] *= (1 + power[k]);
}
#ifndef ONLINE_JUDGE
printf ("%d has %d factors\n", i, factors[i - L]);
#endif
}
int max_factor_num = L;
for (int i=L; i<=U; ++i) {
if (factors[i - L] > factors[max_factor_num - L]) {
max_factor_num = i;
}
}
printf ("Between %d and %d, %d has a maximum of %d divisors.\n",
L, U, max_factor_num, factors[max_factor_num - L]);
}
return 0;
}