Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <mutex>
- #include <thread>
- using namespace std;
- using llong = long long;
- int main() {
- unsigned num_cpus = std::thread::hardware_concurrency();
- // std::cout << "Launching " << num_cpus << " threads\n";
- // A mutex ensures orderly access to std::cout from multiple threads.
- std::mutex iomutex;
- std::vector<std::thread> threads(num_cpus);
- int q;
- cin >> q;
- for (int i = 0; i < q; i++)
- {
- llong n;
- cin >> n;
- llong count = 0;
- llong cur = 1;
- auto startTime = std::chrono::system_clock::now();
- for (unsigned i = 0; i < num_cpus; ++i)
- {
- threads[i] = std::thread([&iomutex, &cur, &count, n, i]()
- {
- while (cur < n)
- {
- llong a = 0;
- {
- std::lock_guard<std::mutex> iolock(iomutex);
- a = cur++;
- //cout << cur << endl;
- }
- //llong a = cur++;
- bool earlyOut = false;
- llong a2 = a * a;
- llong a3 = a2 * a;
- llong aval = (8LL * a3) + (15LL * a2) + (6LL * a);
- for (llong b = n - a; b > 0; b--)
- {
- llong b2 = b * b;
- for (llong c = n - a - b; c > 0; c--)
- {
- auto result = aval - (27LL * b2*c);
- //auto result = getCardanoResult(a, b, c);
- //cout << "{" << a << "," << b << "," << c << "}" << endl;
- //cout << result << endl;
- if (result < n)
- {
- //cout << result << endl;
- }
- earlyOut = result > 1;
- if (earlyOut)
- {
- //cout << "c early out" << endl;
- break;
- }
- if (result == 1)
- {
- //std::lock_guard<std::mutex> iolock(iomutex);
- //cout << "{" << a << "," << b << "," << c << "}" << endl;
- count++;
- }
- }
- }
- }
- });
- }
- for (auto& t : threads) {
- t.join();
- }
- cout << count << endl;
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement