結果

問題 No.793 うし数列 2
ユーザー 826814741_6826814741_6
提出日時 2019-03-05 19:59:29
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,856 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 5,216 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 18:57:40
合計ジャッジ時間 1,085 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local ffi = require 'ffi'
local M = ffi.load 'ssl'
ffi.cdef [[
typedef struct bignum_st {
	uint64_t *d;
	int top;
	int dmax;
	int neg;
	int flags;
} BN;
typedef BN * BN_ptr;
typedef const BN * BN_srcptr;

BN_ptr BN_new(void);
void BN_free(BN_ptr);

int BN_set_word(BN_ptr, uint64_t);
int BN_sub_word(BN_ptr, uint64_t);

char * BN_bn2dec(BN_srcptr);
void CRYPTO_free(void *, const char *, int);

typedef struct bignum_pool_item {
	BN vals[16]; // vals[BN_CTX_POOL_SIZE];
	struct bignum_pool_item *prev, *next;
} BN_POOL_ITEM;
typedef struct bignum_pool {
	BN_POOL_ITEM *head, *current, *tail;
	unsigned used, size;
} BN_POOL;
typedef struct bignum_ctx_stack {
	unsigned int *indexes;
	unsigned int depth, size;
} BN_STACK;
typedef struct bignum_ctx {
	BN_POOL pool;
	BN_STACK stack;
	unsigned int used;
	int err_stack;
	int too_many;
	int flags;
} BN_CTX;
typedef BN_CTX * BN_CTX_ptr;

BN_CTX_ptr BN_CTX_new(void);
void BN_CTX_free(BN_CTX_ptr);

int BN_mod_mul(BN_ptr, BN_srcptr, BN_srcptr, BN_srcptr, BN_CTX_ptr);
int BN_mod_exp(BN_ptr, BN_srcptr, BN_srcptr, BN_srcptr, BN_CTX_ptr);
]]

function bn2str(bn)
	return ffi.string(ffi.gc(M.BN_bn2dec(bn),function (a) M.CRYPTO_free(a,"",0) end))
end

function ul2bn(ul)
	local bn = ffi.gc(M.BN_new(),M.BN_free)
	if ul~=nil then M.BN_set_word(bn,ul) end
	return bn
end

function getCTX()
	return ffi.gc(M.BN_CTX_new(),M.BN_CTX_free)
end

local readUL = (function ()
	ffi.cdef 'int scanf(const char *, ...);'
	local C,c = ffi.C,ffi.new("uint64_t[1]")
	return function () C.scanf("%ld",c) return c[0] end
end)()

do
local ctx,D = getCTX(),1000000007ULL
local b,d,t = ul2bn(),ul2bn(D),ul2bn()

M.BN_mod_exp(b, ul2bn(10ULL), ul2bn(readUL()), d, ctx)
M.BN_mod_mul(b, b, ul2bn(4ULL), d, ctx)
M.BN_sub_word(b, 1ULL)

M.BN_mod_exp(t, ul2bn(3ULL), ul2bn(D-2ULL), d, ctx)
M.BN_mod_mul(b, b, t, d, ctx)

print(bn2str(b))
end
0