結果

問題 No.9000 Hello World! (テスト用)
ユーザー kimiyukikimiyuki
提出日時 2017-05-18 03:00:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,311 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 45,728 KB
最終ジャッジ日時 2023-08-26 08:21:15
合計ジャッジ時間 763 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: コンストラクタ ‘dynamic_library::dynamic_library(const std::string&)’ 内:
main.cpp:15:37: エラー: invalid use of incomplete type ‘const std::string’ {aka ‘const class std::__cxx11::basic_string<char>’}
   15 |         handle = __libc_dlopen_mode(path.c_str(), rtld_now);
      |                                     ^~~~
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/iosfwd:39,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/bits/std_thread.h:38,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/thread:43,
         次から読み込み:  main.cpp:2:
/usr/local/gcc7/include/c++/12.2.0/bits/stringfwd.h:72:11: 備考: declaration of ‘std::string’ {aka ‘class std::__cxx11::basic_string<char>’}
   72 |     class basic_string;
      |           ^~~~~~~~~~~~
main.cpp: メンバ関数 ‘void* dynamic_library::operator()(const std::string&)’ 内:
main.cpp:18:37: エラー: invalid use of incomplete type ‘const std::string’ {aka ‘const class std::__cxx11::basic_string<char>’}
   18 |         return __libc_dlsym(handle, symbol.c_str());
      |                                     ^~~~~~
/usr/local/gcc7/include/c++/12.2.0/bits/stringfwd.h:72:11: 備考: declaration of ‘std::string’ {aka ‘class std::__cxx11::basic_string<char>’}
   72 |     class basic_string;
      |           ^~~~~~~~~~~~
main.cpp: 大域スコープ:
main.cpp:24:44: エラー: no matching function for call to ‘dynamic_library::dynamic_library(const char*&)’
   24 | dynamic_library pthread_handle(pthread_path);
      |                                            ^
main.cpp:13:5: 備考: 候補: ‘dynamic_library::dynamic_library(const std::string&)’
   13 |     dynamic_library(string const & path) {
      |     ^~~~~~~~~~~~~~~
main.cpp:13:36: 備考:   no known conversion for argument 1 from ‘const char*’ to ‘const std::string&’ {aka ‘const std::__cxx11::basic_string<char>&

ソースコード

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