简体   繁体   English

两个数字的公因数总数 LARGE VALUES up to 10^12

[英]Total number of common factors for two numbers LARGE VALUES upto 10^12

Inputs are two values 1 <= m , n <= 10^12 i don't know why my code is taking soo long for large values .输入是两个值 1 <= m , n <= 10^12 我不知道为什么我的代码对于大值需要很长时间。 time limit is 1 sec.时间限制为 1 秒。 please suggest me some critical modifications.请给我建议一些重要的修改。

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
unsigned long long m,n,count=0;
cin >> m >> n;  
for (long long int i = 1; i <= ((min(m,n))/2)+1; i++) //i divided min(m,n) by 2 to make it efficient.
{

    if ((m%i == 0) && (n%i == 0))
    {
        count++;
    }

}
if (((n%m == 0) || (m%n == 0)) && (n!=m))
{
    cout << count << endl;
}

printf("%lld",count);  //cout<<count;

system("pause");
return 0;

} }

Firstly首先

((min(m, n)) / 2) + 1

Is being calculated every iteration.每次迭代都在计算。 But it's loop-invariant.但它是循环不变的。 In general loop invariant code can be calculated before the loop, and stored.一般来说,循环不变代码可以在循环之前计算并存储。 It will add up, but there are obviously much better ways to improve things.它会加起来,但显然有更好的方法来改进。 I'll describe one below:我将在下面描述一个:

you can make this much faster by calculating how many common prime factors there are, and by dividing out any "found" primes as you go.你可以通过计算有多少共同的素数因子,并在你进行时除掉任何“找到”的素数,从而使这个过程更快。 eg if only one number is divisible by 5, and the other is not, you can divide that one by 5 and you still get the same answer for common factors.例如,如果只有一个数能被 5 整除,而另一个不能被 5 整除,那么您可以将一个数除以 5,对于公因数,您仍然得到相同的答案。 Divide m and n by any "found" numbers as you go through it.将 m 和 n 除以任何“找到”的数字。 (but keep checking whether either is divisible by eg 2 and keep dividing before you go on). (但继续检查是否可以被 2 整除,并在继续之前继续除法)。

eg if the two numbers are both divisible by 2, 3 and 5, then the number of ways those three primes can combine is 8 (2^3), treating the presence of each prime as a true/false thing.例如,如果两个数都可以被 2、3 和 5 整除,那么这三个素数可以组合的方式数是 8 (2^3),将每个素数的存在视为真/假。 So each prime that occurs once multiplies the number of combos by 2.因此,出现一次的每个素数都将组合数乘以 2。

If any of the primes occurs more than once, then it changes the equation slightly.如果任何素数出现不止一次,那么它会稍微改变方程。 eg if the two numbers are divisible by 4, 3, 5:例如,如果这两个数字可以被 4、3、5 整除:

4 = 2^2, so you could have no "2s", 1 "2" or 2 "2s" in the combined factor, so the total combinations 3 x 2 x 2 = 12. So any prime that occurs "x" times, multiplies the total number of combos by "x+1". 4 = 2^2,因此组合因子中可能没有“2s”、1 个“2”或 2 个“2s”,因此总组合为 3 x 2 x 2 = 12。所以任何出现“x”次的素数, 将组合总数乘以“x+1”。

So basically, you don't need to check for every actual factor, you just need to search for how many common prime factors there are, then work out how many combos that adds up to.所以基本上,你不需要检查每一个实际因素,你只需要搜索有多少共同的质因数,然后计算出加起来有多少组合。 Luckily you only need to store one value, "total_combos" and multiply it by the "x+1" value for each found number as you go.幸运的是,您只需要存储一个值“total_combos”,然后将其乘以每个找到的数字的“x+1”值。

And a handy thing is that you can divide out all primes as they're found, and you're guaranteed that the largest remaining prime to be found is no larger than the square root of the smallest remaining number out of m and n.一个方便的事情是,您可以在找到所有素数时将它们除掉,并且您可以保证要找到的最大剩余素数不大于 m 和 n 中剩余的最小数的平方根。

So to run you through how this would work, start with a copy of m and n, loop up to the sqrt of the min of those two (m and n will be reduced as the loop cycles through).因此,为了让您了解这是如何工作的,从 m 和 n 的副本开始,循环到这两者的 min 的 sqrt(m 和 n 将随着循环循环而减少)。

make a value "total_combos", which starts at 1.创建一个值“total_combos”,从 1 开始。

Check for 2's first, find out how many common powers of 2 there are, add one to that number.首先检查 2,找出 2 的常见幂有多少,在该数字上加一个。 Divide out ALL the 2's from m and n, even if they're not matched, because reducing down the number cuts the total amount you actually need to search.从 m 和 n 中除掉所有 2,即使它们不匹配,因为减少数字会减少您实际需要搜索的总量。 You count the 2's, add one, then multiply "total_combos" by that.你数数 2,加 1,然后乘以“total_combos”。 Keep dividing m or n by two as long as either has a factor of 2 remaining.继续将 m 或 n 除以 2,只要它们中的任何一个都剩下 2 的因数。

Then check for 3's, find out how many common powers of 3 there are, add one, the multiply "total_combos" by that.然后检查 3,找出 3 的常见幂有多少,加一个,乘以“total_combos”。 Divide out any and all factors of 3 when you're doing this.执行此操作时,除以 3 的所有因数。

then check for 4's.然后检查4。 Since 4 isn't prime and we got rid of all 2's already, there will be zero 4's.由于 4 不是质数,而且我们已经去掉了所有 2,因此 4 将为零。 Add one to that = 1, then we times "total_combos" by 1, so it stays the same.将其加一 = 1,然后我们将“total_combos”乘以 1,因此它保持不变。 We didn't need to check whether 4 was prime or not, the divisions we already did ensured it's ignored.我们不需要检查 4 是否是质数,我们已经做过的除法确保它被忽略。 Same for any power of 2.对于任何 2 的幂都相同。

then check for 5's.然后检查5。 same deal as 2's and 3's.与 2 和 3 的交易相同。 And so on.等等。 All the prime bases get divided out as you go, so whenever a value actually matches you can be sure it's a new prime.随着你的进行,所有的质数碱基都会被分开,所以只要一个值真正匹配,你就可以确定它是一个新的质数。

stop the loop when it exceeds sqrt(max(m,n)) (EDITED: min is probably wrong there).当它超过 sqrt(max(m,n)) 时停止循环(编辑:min 可能是错误的)。 But m and n here are the values that have had all the lower primes divided out, so it's much faster.但是这里的 m 和 n 是将所有较低的素数除掉的值,因此速度要快得多。

I hope this approach is helpful.我希望这种方法有帮助。

There is a better way to solve this problem.有一个更好的方法来解决这个问题。 All you have to do is take the GCD of two numbers .您所要做的就是取两个数字GCD Now any number won't divide m & n if they are greater than their GCD.现在,如果它们大于它们的 GCD,任何数字都不会除以 m & n。 So all you to do is that run a loop till the i<=Math.sqrt(GCD(m,n)) and check if the m%i==0 and n%i==0 only.所以你要做的就是运行一个循环直到i<=Math.sqrt(GCD(m,n))并检查是否只有 m%i==0 和 n%i==0。 It will save a lot of nanosecs.它将节省大量纳秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找两个数最大为10 ^ 6的公约数的最有效方法 - Most efficient way to find the common divisors of two numbers upto 10^6 找出n个数字的公因子数(不包括“ 1”作为公因子) - Find the number of common factors for n numbers excluding “1” as common factor 生成直至给定数字N的步进数 - Generate stepping numbers upto a given number N 尝试使用递归找到一个数的所有因子,最后出现大数分割错误 - Trying to find all factors of a number using recursion, end up with a segmentation fault for large numbers 我试图通过我的代码找到任何数字的质因数。 但是对于大数字,我的代码不会终止。 为什么? - I'm trying to find prime factors of any number through my code. But for large numbers, my code is not terminating. Why? 高达 pow(10,19) 的值的整数溢出 - Integer-Overflow for values upto pow(10,19) 奇数/偶数总数 - Total number of odd/even numbers [L, R] 中具有奇数个奇数因子的数字个数 - Number of numbers in [L, R] with an odd number of odd factors 10^12以下的素数之和 - Sum of prime numbers below 10^12 查找两个大数x和y之间的质数的最快方法 - fastest method for finding number of prime numbers between two large numbers x and y
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM