1716: Divisors
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 8192K | 317 | 113 | Standard |
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.', where L, 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.
//约数个数定理:设n的标准质因数分解为n=p1^a1*p2^a2*...*pm^am, 则n的因数个数=(a1+1)*(a2+1)*...*(am+1).
#include<stdio.h>
#include<memory.h>
#include<math.h>
int main()
{
int isPrime[31630],prime[10000];
int i,j,k,cnt,n,u,l,x,temp,num;
for(i=0;i<31630;i++)
isPrime[i]=1;
isPrime[0]=isPrime[1]=0;
for(i=4;i<31630;i+=2)
isPrime[i]=0;
for(i=3;i<180;i++)
for(j=i*i;j<31630;j+=2*i)
isPrime[j]=0;
cnt=0;
for(i=2;i<31630;i++)
if(isPrime[i])
prime[cnt++]=i;//筛选法求素数
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&l,&u);
int max=0,max_i;
int a[10000];
for(i=l;i<=u;i++)
{
memset(a,0,sizeof(a));
x=int(sqrt(i));
k=0;
temp=i;
for(j=0;prime[j]<=x;j++)
{
if(i%prime[j]!=0)
continue;
while(temp%prime[j]==0)
{
a[k]++;
temp/=prime[j];
}
k++;
if(temp==1)
break;
}
if(temp>1)
a[k++]++;
num=1;
for(j=0;j<k;j++)
num*=a[j]+1;
if(num>max)
{
max=num;
max_i=i;
}
}
printf("Between %d and %d, %d has a maximum of %d divisors./n",l,u,max_i,max);
}
return 0;
}
本文介绍了一种高效算法,用于找出指定范围内拥有最多正约数的整数及其约数数量。通过质因数分解和筛选法求素数,实现快速搜索。
789

被折叠的 条评论
为什么被折叠?



