結果

問題 No.1339 循環小数
ユーザー 👑 obakyanobakyan
提出日時 2022-04-14 09:35:35
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 2,550 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 5,296 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-25 19:36:03
合計ジャッジ時間 3,120 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 6 ms
4,384 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 6 ms
4,384 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 10 ms
4,376 KB
testcase_13 AC 14 ms
4,380 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 7 ms
4,384 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 6 ms
4,376 KB
testcase_20 AC 7 ms
4,384 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs

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 t = {}
      t.p = dv
      t.cnt = 1
      x = mfl(x / dv)
      while x % dv == 0 do
        x = mfl(x / dv)
        t.cnt = t.cnt + 1
      end
      table.insert(tmp, t)
      lim = mce(msq(x))
    end
    if primepos == prime_num then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if x ~= 1 then
    local t = {}
    t.p, t.cnt = x, 1
    table.insert(tmp, t)
  end
  return tmp
end

local function getdivisorCore(divisorparts)
  local t = {}
  local pat = 1
  local len = #divisorparts
  local allpat = 1
  for i = 1, len do
    allpat = allpat * (1 + divisorparts[i].cnt)
  end
  for t_i_pat = 0, allpat - 1 do
    local div = allpat
    local i_pat = t_i_pat
    local ret = 1
    for i = 1, len do
      div = mfl(div / (divisorparts[i].cnt + 1))
      local mul = mfl(i_pat / div)
      i_pat = i_pat % div
      for j = 1, mul do
        ret = ret * divisorparts[i].p
      end
    end
    table.insert(t, ret)
  end
  table.sort(t)
  return t
end

local function getdivisor(x, primes)
  local dvp = getdivisorparts(x, primes)
  return getdivisorCore(dvp)
end

local primes = getprimes(mce(msq(1000000007)))

local function modpow(src, pow, mod)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = (res * src) % mod
      pow = pow - 1
    end
    src = (src * src) % mod
    pow = mfl(pow / 2)
  end
  return res
end

local q = io.read("*n")
local function solve(n)
  while n % 2 == 0 do
    n = mfl(n / 2)
  end
  while n % 5 == 0 do
    n = mfl(n / 5)
  end
  if n == 1 then return 1 end
  local dvp = getdivisorparts(n, primes)
  local euler = n
  for i = 1, #dvp do
    local v = dvp[i].p
    euler = mfl(euler * (v - 1) / v)
  end
  local dv = getdivisor(euler, primes)
  for i = 1, #dv do
    if modpow(10, dv[i], n) == 1 then
      return dv[i]
    end
  end
end
for iq = 1, q do
  local n = io.read("*n")
  print(solve(n))
end
0