結果

問題 No.9000 Hello World! (テスト用)
ユーザー kimiyuki
提出日時 2017-05-18 03:00:09
言語 C++14
(gcc 6.3.0)
結果
AC  
実行時間 4794 ms
コード長 2311 Byte
コンパイル時間 841 ms
使用メモリ 1364 KB

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
1.txt AC 4770 ms
1360 KB
2.txt AC 4794 ms
1360 KB
3.txt AC 4762 ms
1364 KB
4.txt AC 4755 ms
1364 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <cstdio>
#include <thread>
#include <cassert>
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;
}
0