题目:
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.
题意:
输入两个整数L、H其中(1≤L≤H≤109,H−L≤100001≤L≤H≤109,H−L≤10000),统计[L,H]区间上正约数最多的那个数P(如有多个,取最小值)以及P的正约数的个数D。
素因数分解:对于任意的一个正整数N,若有N=p1^e1*p2^e2*...pr^er, 且p1、p2...pr都为素数,则有N的因数个数为(e1+1)(e2+1)...(er+1)(e1+1)(e2+1)...(er+1)。
思路:考虑先素数筛法打表得出32000以内所有素数,然后把因数个数求出来,若大于之前出现过的最大因子数,则修改;反之,不变。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const int N=333333;
int p,num;
int prime[N+20],vis[N+20],cnt=0;
int ans;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
ll solve(ll n)
{
ll m=n;
ll ans=1;
for(int i=0; i<cnt&&prime[i]<=n; i++) //素因数分解
{
int p=0;
while(n%prime[i]==0)
{
n/=prime[i];
p++;
}
ans*=(p+1);
}
return ans;
}
int main()
{
memset(vis,0,sizeof(vis));
for (int i = 2; i < N; i++) //打表32000以内的素数(快速线性筛)
{
if (!vis[i])
prime[cnt++] = i;
for (int j = 0; j < cnt&&i*prime[j]<N; j ++)
{
vis[i*prime[j]]=1;
if(!(i%prime[j]))
break;
}
}
ll l,r;
int t;
cin>>t;
while(t--)
{
cin>>l>>r;
ll pos,ans=0;
for(int i=l; i<=r; i++)
{
ll res=solve(i);
if(res>ans) //更新最大值
{
pos=i;
ans=res;
}
}
printf("Between %lld and %lld, %lld has a maximum of %lld divisors.\n",l,r,pos,ans);
}
return 0;
}