Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <mutex>
  7. #include <thread>
  8.  
  9. using namespace std;
  10. using llong = long long;
  11.  
  12. int main() {
  13. unsigned num_cpus = std::thread::hardware_concurrency();
  14. // std::cout << "Launching " << num_cpus << " threads\n";
  15.  
  16. // A mutex ensures orderly access to std::cout from multiple threads.
  17. std::mutex iomutex;
  18. std::vector<std::thread> threads(num_cpus);
  19. int q;
  20. cin >> q;
  21. for (int i = 0; i < q; i++)
  22. {
  23. llong n;
  24. cin >> n;
  25. llong count = 0;
  26. llong cur = 1;
  27. auto startTime = std::chrono::system_clock::now();
  28. for (unsigned i = 0; i < num_cpus; ++i)
  29. {
  30. threads[i] = std::thread([&iomutex, &cur, &count, n, i]()
  31. {
  32. while (cur < n)
  33. {
  34. llong a = 0;
  35. {
  36. std::lock_guard<std::mutex> iolock(iomutex);
  37. a = cur++;
  38. //cout << cur << endl;
  39. }
  40. //llong a = cur++;
  41. bool earlyOut = false;
  42. llong a2 = a * a;
  43. llong a3 = a2 * a;
  44. llong aval = (8LL * a3) + (15LL * a2) + (6LL * a);
  45. for (llong b = n - a; b > 0; b--)
  46. {
  47. llong b2 = b * b;
  48. for (llong c = n - a - b; c > 0; c--)
  49. {
  50. auto result = aval - (27LL * b2*c);
  51. //auto result = getCardanoResult(a, b, c);
  52. //cout << "{" << a << "," << b << "," << c << "}" << endl;
  53. //cout << result << endl;
  54. if (result < n)
  55. {
  56. //cout << result << endl;
  57. }
  58. earlyOut = result > 1;
  59. if (earlyOut)
  60. {
  61. //cout << "c early out" << endl;
  62. break;
  63. }
  64. if (result == 1)
  65. {
  66. //std::lock_guard<std::mutex> iolock(iomutex);
  67. //cout << "{" << a << "," << b << "," << c << "}" << endl;
  68. count++;
  69. }
  70. }
  71. }
  72. }
  73. });
  74. }
  75.  
  76. for (auto& t : threads) {
  77. t.join();
  78. }
  79.  
  80. cout << count << endl;
  81.  
  82. return 0;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement