結果

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

テストケース

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

ソースコード

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);

BN_ptr BN_copy(BN_ptr, BN_srcptr);

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_div(BN_ptr, BN_ptr, BN_srcptr, BN_srcptr, BN_CTX_ptr);
int BN_mod_add(BN_ptr, BN_srcptr, BN_srcptr, BN_srcptr, 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 bn2num(bn)
	return tonumber(bn2str(bn))
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("long[1]")
	return function () C.scanf("%ld",c) return c[0] end
end)()

do
local ctx,d,ul = getCTX(),ul2bn(1000000007ULL),readUL()
local a,b = ul2bn(),ul2bn()

M.BN_mod_exp(a, ul2bn(10ULL), ul2bn(ul), d, ctx)
M.BN_copy(b, a)

M.BN_sub_word(b, 1ULL)
M.BN_div(b, nil, b, ul2bn(9ULL), ctx)
-- M.BN_div(nil, b, b, d, ctx)
M.BN_mod_mul(b, b, ul2bn(3ULL), d, ctx)

M.BN_mod_add(a, a, b, d, ctx)

print(bn2str(a))
end
0