#include #include #include #include using namespace std; void parallel() { using ll = long long; const int nthread = thread::hardware_concurrency(); vector threads; mutex mtx; const ll MOD = 1e8 + 7; const ll A = 1, B = MOD; ll prod = 1; for (int i = 0; i < nthread; ++i) { ll beg = A + (B - A) * i / nthread; ll end = A + (B - A) * (i + 1) / nthread; auto product = [&prod, &mtx, beg, end] { ll q = 1; for (ll i = beg; i < end; ++i) q = q * i % MOD; mtx.lock(); prod = prod * q % MOD; mtx.unlock(); }; threads.push_back(thread(product)); } for (auto& thread : threads) thread.join(); fprintf(stderr, "%llu\n", prod); puts("Hello World!"); } int main() { auto beg = chrono::system_clock::now(); auto ubeg = clock(); parallel(); auto end = chrono::system_clock::now(); auto uend = clock(); auto elapsed = chrono::duration_cast(end - beg).count(); fprintf(stderr, "elapsed: %.3f (%.3f) sec.\n", elapsed / 1000.0, double(uend - ubeg) / CLOCKS_PER_SEC ); }