除数的范围内编号[A,B],a和b可以是大量除数、范围内、编号

2023-09-11 06:37:50 作者:笙

假设查找1和N之间的所有数字的约数的数量,我们使用:

 为(i = 1; I< N ++ I)
    为(J =; J&所述N; J + =ⅰ)
        因素分析[J] ++;
 

我们应该怎么做,如果范围是像 [A,B] ,使得 1< A,B< 10 ^ 9和B-A其中;万? 如果我们调整上面的code:

 为(i = 1; I< B; ++ I)
    为(J =; J< B; J + = 1)
        因素分析[J] ++;
 

要花太多时间,如果 B运行= 10 ^ 9 。那么什么样的优化可制成,因为巴是相对于a和b小,和a和b是大的(10 ^ 9或更多)?

PS。也许我无法解释这个问题很好。我真的需要找到的是多少个号码的范围有除数等于某些数x<。= 100

感谢。的

解决方案

最基本的答案是计算利息每个数字的分解,然后用它来计算除数的数量:如果分解为^ W * B ^ x的* C ^ý* D ^ Z然后除数的数目为(W + 1)*(X + 1)*(Y + 1)*(Z + 1)。

在有有理数范围内规定法则 a b c 1 2 a b c a b c

您可以在几种方法的分解。一种方式是由审判庭;自从10 ^ 9的限制小,审判庭会工作,没有太多的痛苦。另一种方式是通过筛分,使用埃拉托塞尼的分段筛找到的因素,然后计算它们的多重性由分割

您可以找到算法和code审理分工,埃拉托色尼的分割筛,并计算在一个数的约数我博客。点击菜单栏上的练习,然后主题,然后选择素数。

编辑:下面是我会怎么办呢

的第一步是筛为范围内的所有数字的因素。计算比的 B 的平方根使用埃拉托色尼的筛少的素数。创建长度的数组的 B 的 - 的在的+ 1初始化为空列表中的每个元素。对于比的平方根每个首要以下B 的计算第一数目的 K 的大于或等于一的是这样的素数的倍数,然后为每个数组元素开始的 K 的 - 的在的和在间隔P - K 的 - 的一个 + P , K 的 - 的在的+ 2 P , K - 在的+ 3 P 的,等等 - 增加的 P 的到列表在阵列的位置。这使你的范围内的所有数字的因素的清单,但不是他们的多重性。

第二步骤是计算在范围内的所有的数字因数的数目。每个元素的我的数组包含一个列表。如果列表是空的,这个数字是素数,而且数量的在的+的我的有两个约数。否则,使用试验除以所述已知因素中的列表,以确定它们的多重性,并使用这些多重计算除数的数

然后你只指望那些在范围内拥有一批约数相等的 X 的。

Suppose for finding number of divisors of all numbers between 1 and N, we use:

for (i = 1; i < N; ++i) 
    for (j = i; j < N; j += i) 
        factors[j]++;

What should we do if the range is something like [a,b] such that 1 < a,b < 10^9 and b-a < 10,000? If we adapt the code above as:

for (i = 1; i < b; ++i)
    for (j = i; j < b; j += i)
        factors[j]++;

it would take too much time to run if b = 10^9. So what kind of optimization can be made, given that b-a is small relative to a and b, and a and b are large (10^9 or more)?

PS. Maybe I couldn't explain the problem well. What I actually need to find is how many numbers in the range have number of divisors equal to some x <= 100.

Thanks.

解决方案

The basic answer is to calculate the factorization of each number of interest, then use that to calculate the number of divisors: if the factorization is a^w * b^x * c^y * d^z then the number of divisors is (w+1) * (x+1) * (y+1) * (z+1).

You can find the factorization in a couple of ways. One way is by trial division; since the limit of 10^9 is small, trial division will work without too much pain. Another way is by sieving, using a segmented Sieve of Eratosthenes to find the factors, then calculating their multiplicities by division.

You can find algorithms and code for trial division, the segmented Sieve of Eratosthenes, and calculating the divisors of a number on my blog. Click on Exercises on the menu bar, then Themes, then select Prime Numbers.

EDIT: Here's how I would do it.

The first step is to sieve for the factors of all the numbers in the range. Calculate the primes less than the square root of b using the Sieve of Eratosthenes. Create an array of length b - a + 1 with each element initialized to an empty list. For each prime less than the square root of b calculate the first number k greater than or equal to a that is a multiple of the prime, then for each array element starting at k - a and at intervals of p -- k - a + p, k - a + 2 p, k - a + 3 p, and so on -- add p to the list at the array location. That gives you a list of the factors of all the numbers in the range, but not their multiplicities.

The second step is to calculate the number of divisors of all the numbers in the range. Each element i of the array contains a list. If the list is empty, the number is prime, and the number a + i has two divisors. Otherwise, use trial division by the known factors in the list to determine their multiplicities, and use those multiplicities to calculate the number of divisors.

Then you just count those in the range with a number of divisors equal to x.