简体   繁体   English

C ++共质问题。 优化代码

[英]C++ Coprimes Problem. Optimize code

Hi i want to optimize the following code. 嗨,我想优化以下代码。 It tries to find all coprimes in a given range by comparing them to n. 它尝试通过将它们与n进行比较来查找给定范围内的所有互质。 But i want to make it run faster... any ideas? 但是我想让它运行得更快...有什么想法吗?

#include <iostream>

using namespace std;

int GCD(int a, int b)
{
    while( 1 )
    {
        a = a % b;
  if( a == 0 )
   return b;
  b = b % a;

        if( b == 0 )
   return a;
    }
}


int main(void){
 int t;
 cin >> t;
 for(int i=0; i<t; i++){
  int n,a,b;
  cin >> n >> a >> b;

  int c = 0;
  for(int j=a; j<=b; j++){
   if(GCD(j, n) == 1) c++;
  }

  cout << c << endl;
 }

 return 0;
}

This smells like homework, so only a hint. 这闻起来像作业,所以只是一个提示。

You don't need to calculate GCD here. 您无需在此处计算GCD。 If you can factorize n (even in the crudest way of trying to divide by every odd number smaller than 2^16), then you can just count numbers which happen not to divide by factors of n. 如果您可以分解n(即使以试图除以小于2 ^ 16的每个奇数除以最粗略的方式),那么您也可以仅对碰巧不被n除以的数进行计数。

Note that there will be at most 10 factors of a 32-bit number (we don't need to remember how many times given prime is used in factorization). 请注意,一个32位数字最多包含10个因数(我们不需要记住给定质数在因式分解中使用了多少次)。

How to do that? 怎么做? Try to count non-coprimes using inclusion–exclusion principle. 尝试使用包含-排除原理来计算非余数。 You will have at most 1023 subsets of primes to check, for every subset you need to calculate how many multiplies are in the range, which is constant time for each subset. 您最多要检查1023个素数子集,对于每个子集,您需要计算范围内有多少个乘法,这是每个子集的恒定时间。

Anyway, my code works in no time now: 无论如何,我的代码现在可以立即使用:

liori:~/gg% time ./moje <<< "1 1003917915 1 1003917915"
328458240
./moje <<< "1 1003917915 1 1003917915"  0,00s user 0,00s system 0% cpu 0,002 total

On a single core computer it's not going to get much faster than it currently is. 在单核计算机上,它不会比现在快得多。 So you want to utilize multiple cores or even multiple computers. 因此,您想利用多个内核甚至多个计算机。 Parallelize and distribute. 并行分发。

Since each pair of numbers you want to calculate the GCD for isn't linked to any other pair of numbers you can easily modify your program to utilize multiple cores by using threads. 由于您要为其计算GCD的每对数字均未链接到任何其他对数字,因此您可以通过使用线程轻松地修改程序以利用多个内核。

If this still isn't fast enough you'd better start thinking of using distributed computing, assigning the work to many computers. 如果仍然不够快,您最好开始考虑使用分布式计算,将工作分配给多台计算机。 This is a bit trickier but should improve the performance the most if the search space is large. 这有点棘手,但是如果搜索空间很大,则应该可以最大程度地提高性能。

Consider giving it a try with double s. 考虑尝试使用double s。 It said that divisions with doubles are faster on typical intel chips. 它说,在典型的英特尔芯片上,双倍除法更快。 Integer division is the slowest instruction out there. 整数除法是目前最慢的指令。 This is a chicken egg problem. 这是一个鸡蛋问题。 Nobody uses them because they're slow and intel doesnt make it faster because nobody uses it. 没有人使用它们是因为它们速度慢,而intel却没有使其更快,因为没有人使用它。

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

相关问题 Code::Blocks 和 C++ 编译器问题。 无法编译头文件 - Code::Blocks, and C++ Compiler problem. cant compile a header C ++共同祖先问题。 它是什么? - C++ Common Ancestor Problem. What is it? 循环问题。 Cin C ++ Getline清除缓冲区 - Loop problem. Cin C++ getline clear buffer 提交 Kattis “OddGnome”问题时出错。 我在本地得到正确的输出,我的代码有什么问题? C++ - Getting error on submission for Kattis "OddGnome" problem. I am getting the right output locally, what is wrong with my code? C++ Objective C和c ++导入问题。 很简单,但我不明白 - Objective C and c++ import problem. Simple but I don't get it C ++代码问题 - C++ code problem 链表 - AppendLastNToFirst (C++),我只写了问题的逻辑。 请告诉我更正 - Linked list - AppendLastNToFirst (C++), I have written only the logic for the problem. Please tell me the correction C ++:可能的Xcode(Mac)问题。 无法使用getline()从外部文件中读取数字 - C++: possible Xcode (Mac) problem. Can't read in numbers from external file using getline() C++ 设置问题。 output 不对齐,它移动了几个空格,这是为什么? - C++ setw problem. The output is not align and its is shifted a few space why is that? c ++尝试通过替换测试来优化代码 - c++ attempt to optimize code by replacing tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM