結果

問題 No.303 割れません
ユーザー antaanta
提出日時 2016-04-25 06:51:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,539 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 67,216 KB
最終ジャッジ日時 2024-04-27 04:56:08
合計ジャッジ時間 1,339 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/usr/bin/ld: /tmp/cc9M0Ohm.o: in function `DynamicLibrary::get(char const*) const [clone .isra.0]':
main.cpp:(.text.startup+0x8): undefined reference to `__libc_dlsym'
/usr/bin/ld: /tmp/cc9M0Ohm.o: in function `_GLOBAL__sub_I_gmp':
main.cpp:(.text.startup+0x263): undefined reference to `__libc_dlopen_mode'
collect2: error: ld returned 1 exit status

ソースコード

diff #

#include <string>
#include <iostream>

#include <gmp.h>

using namespace std;

#if defined(_WIN32)
extern "C" {
	void* __stdcall LoadLibraryA(const char *name);
	void* __stdcall GetProcAddress(void *handle, const char *name);
}
struct DynamicLibrary {
	DynamicLibrary(const char *name) { _handle = LoadLibraryA(name); }
	void *get(const char *name) const { void *p = GetProcAddress(_handle, name); if(!p) cerr << "can't find: " << name << endl; return p; }
	void *_handle;
} gmp("mpir.dll");
#else
extern "C" {
	void* __libc_dlopen_mode(const char *x, int y);
	void* __libc_dlsym(void *x, const char *y);
}
struct DynamicLibrary {
	DynamicLibrary(const char *name) { _handle = __libc_dlopen_mode(name, 2); }
	void *get(const char *name) const { void *p = __libc_dlsym(_handle, name); if(!p) cerr << "can't find: " << name << endl; return p; }
	void *_handle;
} gmp("/usr/lib64/libgmp.so.10");
#endif

#define DECL_GMP(name) const auto my_##name = (decltype(::name)*)gmp.get("__g" #name)

DECL_GMP(mpz_init);
DECL_GMP(mpz_clear);
DECL_GMP(mpz_set);
DECL_GMP(mpz_set_ui);
DECL_GMP(mpz_add);
DECL_GMP(mpz_add_ui);
DECL_GMP(mpz_sub);
DECL_GMP(mpz_sub_ui);
DECL_GMP(mpz_mul);
DECL_GMP(mpz_mul_ui);
DECL_GMP(mpz_sizeinbase);
DECL_GMP(mpz_get_str);

void fib_pair(int n, mpz_t res_a, mpz_t res_b) {
	if(n <= 2) {
		my_mpz_set_ui(res_a, (n + 1) / 2);
		my_mpz_set_ui(res_b, (n + 2) / 2);
		return;
	}
	mpz_t a, b, c, d;
	my_mpz_init(a);
	my_mpz_init(b);
	my_mpz_init(c);
	my_mpz_init(d);

	fib_pair(n / 2 - 1, a, b);

	my_mpz_mul(a, a, a);
	my_mpz_mul(b, b, b);

	my_mpz_add(c, b, a);
	my_mpz_mul_ui(d, b, 4);
	my_mpz_sub(d, d, a);
	if(n >> 1 & 1)
		my_mpz_sub_ui(d, d, 2);
	else
		my_mpz_add_ui(d, d, 2);

	if(n & 1) {
		my_mpz_set(res_a, d);
		my_mpz_mul_ui(res_b, d, 2);
		my_mpz_sub(res_b, res_b, c);
	} else {
		my_mpz_sub(res_a, d, c);
		my_mpz_set(res_b, d);
	}

	my_mpz_clear(a);
	my_mpz_clear(b);
	my_mpz_clear(c);
	my_mpz_clear(d);
}

int main() {
	int n;
	while(~scanf("%d", &n)) {
		if(n == 2) {
			puts("3\nINF");
		} else {
			mpz_t ans, a, b;
			my_mpz_init(ans);
			my_mpz_init(a);
			my_mpz_init(b);

			if(n % 2 == 1) {
				fib_pair(n, a, b);
				my_mpz_set(ans, a);
			} else {
				fib_pair(n / 2 - 1, a, b);
				my_mpz_mul(ans, a, b);
				my_mpz_mul_ui(ans, ans, 2);
			}

			size_t bufsize = my_mpz_sizeinbase(ans, 10) + 2;
			char *buf = new char[bufsize];
			my_mpz_get_str(buf, 10, ans);
			printf("%d\n", n);
			puts(buf);
			delete[] buf;

			my_mpz_clear(b);
			my_mpz_clear(a);
			my_mpz_clear(ans);
		}
	}
	return 0;
}
0