結果
| 問題 |
No.1809 Divide NCK
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2022-01-15 01:04:33 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 32 ms / 2,000 ms |
| コード長 | 1,777 bytes |
| コンパイル時間 | 242 ms |
| コンパイル使用メモリ | 6,948 KB |
| 実行使用メモリ | 12,020 KB |
| 最終ジャッジ日時 | 2024-11-20 17:30:52 |
| 合計ジャッジ時間 | 2,902 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs
local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
long long atoll(const char*);
]]
local function lltonumber(str)
return C.atoll(str)
end
local function getprimes(x)
local primes = {}
local allnums = {}
for i = 1, x do allnums[i] = true end
for i = 2, x do
if allnums[i] then
table.insert(primes, i)
local lim = mfl(x / i)
for j = 2, lim do
allnums[j * i] = false
end
end
end
return primes
end
local function getdivisorparts(x, primes)
local prime_num = #primes
local tmp = {}
local lim = mce(msq(x))
local primepos = 1
local dv = primes[primepos]
while primepos <= prime_num and dv <= lim do
if x % dv == 0 then
local cnt = 1
x = mfl(x / dv)
while x % dv == 0 do
x = mfl(x / dv)
cnt = cnt + 1
end
tmp[dv] = cnt
lim = mce(msq(x))
end
if primepos == prime_num then break end
primepos = primepos + 1
dv = primes[primepos]
end
if x ~= 1 then
tmp[x] = 1
end
return tmp
end
local n, k, m = io.read():match("(%d+) (%d+) (%d+)")
n = lltonumber(n)
k = lltonumber(k)
m = tonumber(m)
local primes = getprimes(1000 * 1000)
local dvp = getdivisorparts(m, primes)
local ret = false
for p, cnt in pairs(dvp) do
local tmp = n
local pl = 1LL * p
local z = 0LL
while 0LL < tmp do
z = z + tmp / pl
tmp = tmp / pl
end
tmp = k
while 0LL < tmp do
z = z - tmp / pl
tmp = tmp / pl
end
tmp = n - k
while 0LL < tmp do
z = z - tmp / pl
tmp = tmp / pl
end
z = z / (1LL * cnt)
if not ret then
ret = z
elseif z < ret then
ret = z
end
end
ret = tostring(ret):gsub("LL", "")
print(ret)