#include #include #include using ll = long long; using namespace std; extern "C" { void *__libc_dlopen_mode(const char *x, int y); void *__libc_dlsym(void *x, const char *y); } struct dynamic_library { void *handle; dynamic_library(string const & path) { int rtld_now = 2; handle = __libc_dlopen_mode(path.c_str(), rtld_now); } void *operator () (string const & symbol) { return __libc_dlsym(handle, symbol.c_str()); } }; const char *pthread_path = "/usr/lib64/libpthread.so.0"; // yukicoder // const char *pthread_path = "/lib/x86_64-linux-gnu/libpthread.so.0"; // atcoder dynamic_library pthread_handle(pthread_path); extern "C" { int pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine) (void *), void *__restrict __arg) { typedef decltype(pthread_create) (*type); static type ptr = (type)(pthread_handle("pthread_create")); return ptr(__newthread, __attr, __start_routine, __arg); } void pthread_exit (void *__retval) { typedef decltype(pthread_exit) (*type); static type ptr = (type)(pthread_handle("pthread_exit")); ptr(__retval); } int pthread_join (pthread_t __th, void **__thread_return) { typedef decltype(pthread_join) (*type); static type ptr = (type)(pthread_handle("pthread_join")); return ptr(__th, __thread_return); } int pthread_detach (pthread_t __th) { typedef decltype(pthread_detach) (*type); static type ptr = (type)(pthread_handle("pthread_detach")); return ptr(__th); } } constexpr int mod = 1e9+7; void func(int l, int r, int *result) { ll acc = 1; for (int i = l; i < r; ++ i) { acc = acc * i % mod; } *result = acc; } int main() { int n = 1000000005; int result[4]; constexpr int num_threads = 4; thread th[num_threads]; for (int i = 0; i < num_threads; ++ i) { int l = (n - 1) *(ll) i / num_threads + 1; int r = (n - 1) *(ll) (i+1) / num_threads + 1; th[i] = thread(func, l, r, &result[i]); } ll acc = 1; for (int i = 0; i < num_threads; ++ i) { th[i].join(); acc = acc * result[i] % mod; } assert (acc == 500000003); printf("Hello World!\n"); return 0; }