Divisors UVA - 294 (欧拉筛 + 素因数分解)

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.

 

题意:给定一个区间,求区间内因数个数最大的数,若存在多个最大的因数个数的数,则取最小的数。

解题方法:欧拉筛+素因数分解

素因数分解公式:a =  p1^e1 * p2^e2 * p3^e3 * ... * pn^en 

                             (a为正整数,pi代表素数(可以不相邻),ei为幂数)

则因数个数公式为:Suma = (e1+1)(e2+1)(e3+1)(e4+1)...(en+1)

欧拉筛直接调用模板即可,不过由于数据范围是10^10,所以欧拉筛可以筛到10^5,也可以更少,但不得少于33000。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

const long long N = 100000;
bool ab[N];
long long p[N];
long long n,num;

//素数筛
void prime(){
	memset(ab, 0, n*sizeof ab[0]);
	num = 0;
	ab[0] = ab[1] = 1;
	for(int i = 2; i < N; ++i){
		if(!(ab[i]))
			p[num++] = i;
		for(int j = 0; (j < num && i * p[j] < N); ++j){
			ab[i * p[j]] = 1;
			if(!(i % p[j]))
				break;
		}
	}
}

//素因数分解求因数个数
int number(long long a,long long n){
	int sum = 1;
	for(int i = 0; i < n && a > 1; i++){
		if(a % p[i] == 0){
			int count = 1;
			while(a % p[i] == 0){
				count++;
				a /= p[i];
			}
			sum *= count;
		}
	}
	return sum;
}

int main(){
	prime();
	long long n;
	cin >> n;
	long long a,b;
	while(n--){
		cin >> a >> b;
		long long sa = a,temp,maxs = 0;
		for(int i = a;i <= b; i++){
			temp = number(i, num);
			if(temp > maxs){
				sa = i;
				maxs = temp;
			}
		}
		cout << "Between " << a << " and " << b << ", " << sa << " has a maximum of " << maxs << " divisors.\n";
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值