結果

問題 No.438 Cwwプログラミング入門
ユーザー 👑 obakyanobakyan
提出日時 2021-07-20 21:06:09
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 3,542 bytes
コンパイル時間 111 ms
コンパイル使用メモリ 5,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-24 12:55:42
合計ジャッジ時間 6,736 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 WA -
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 WA -
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 1 ms
4,376 KB
testcase_54 AC 1 ms
4,380 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 1 ms
4,376 KB
testcase_63 AC 1 ms
4,376 KB
testcase_64 AC 1 ms
4,380 KB
testcase_65 AC 1 ms
4,380 KB
testcase_66 AC 1 ms
4,380 KB
testcase_67 AC 1 ms
4,376 KB
testcase_68 WA -
testcase_69 AC 2 ms
4,376 KB
testcase_70 AC 1 ms
4,376 KB
testcase_71 AC 1 ms
4,380 KB
testcase_72 AC 1 ms
4,376 KB
testcase_73 AC 2 ms
4,376 KB
testcase_74 AC 2 ms
4,380 KB
testcase_75 AC 2 ms
4,376 KB
testcase_76 WA -
testcase_77 AC 1 ms
4,380 KB
testcase_78 AC 1 ms
4,376 KB
testcase_79 AC 1 ms
4,380 KB
testcase_80 AC 1 ms
4,376 KB
testcase_81 AC 1 ms
4,376 KB
testcase_82 AC 1 ms
4,376 KB
testcase_83 AC 1 ms
4,380 KB
testcase_84 AC 1 ms
4,380 KB
testcase_85 AC 1 ms
4,380 KB
testcase_86 AC 1 ms
4,376 KB
testcase_87 AC 1 ms
4,380 KB
testcase_88 AC 1 ms
4,376 KB
testcase_89 AC 1 ms
4,380 KB
testcase_90 AC 1 ms
4,376 KB
testcase_91 AC 1 ms
4,380 KB
testcase_92 AC 1 ms
4,376 KB
testcase_93 AC 2 ms
4,380 KB
testcase_94 AC 1 ms
4,376 KB
testcase_95 AC 1 ms
4,380 KB
testcase_96 AC 1 ms
4,376 KB
testcase_97 AC 1 ms
4,380 KB
testcase_98 AC 1 ms
4,376 KB
testcase_99 AC 1 ms
4,376 KB
testcase_100 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mab = math.abs
local function getgcd(x, y)
  while 0 < x do
    x, y = y % x, x
  end
  return y
end

local function bezout_eq(a, b, Z)
  if b == 0 then
    return Z, 0
  end
  local y, x = bezout_eq(b, a % b, Z)
  y = y - mfl(a / b) * x
  return x, y
end

local function triplediv(xmin, xmax, func)
  local left_v, right_v = func(xmin), func(xmax)
  while 1000 < xmax - xmin do
    local xmid1, xmid2 = mfl((xmin * 2 + xmax) / 3), mfl((xmin + xmax * 2) / 3)
    local mid_v1, mid_v2 = func(xmid1), func(xmid2)
    if left_v > mid_v1 then
      if mid_v2 > right_v then
        xmin = xmid2
        left_v = mid_v2
      elseif mid_v1 > mid_v2 then
        xmin = xmid1
        left_v = mid_v1
      else
        xmax = xmid2
        right_v = mid_v2
      end
    else
      if mid_v2 < right_v then
        xmax = xmid1
        right_v = mid_v1
      elseif mid_v1 < mid_v2 then
        xmin = xmid1
        left_v = mid_v1
      else
        xmax = xmid2
        right_v = mid_v2
      end
    end
  end
  -- return (xmin + xmax) / 2
  return xmin, xmax
end


local x, y, z = io.read("*n", "*n", "*n")
if x == 0 and y == 0 then
  if z == 0 then
    print("c")
  else
    print("NO")
  end
elseif x == 0 then
  if z == 0 then
    print("c")
  elseif z % y == 0 then
    local cnt = mfl(z / y)
    if cnt * 2 - 1 <= 10000 then
      io.write(string.rep("w", cnt))
      io.write(string.rep("C", cnt - 1))
      io.write("\n")
    else
      print("NO")
    end
  else
    print("NO")
  end
elseif y == 0 then
  if z == 0 then
    print("w")
  elseif z % x == 0 then
    local cnt = mfl(z / x)
    if cnt * 2 - 1 <= 10000 then
      io.write(string.rep("c", cnt))
      io.write(string.rep("C", cnt - 1))
      io.write("\n")
    else
      print("NO")
    end
  else
    print("NO")
  end
else
  local gcd = getgcd(x, y)
  if z % gcd ~= 0 then
    print("NO") os.exit()
  end
  x, y, z = mfl(x / gcd), mfl(y / gcd), mfl(z / gcd)
  local a1, b1 = bezout_eq(x, y, z)
  -- print(a1, b1)
  if 0 < a1 then
    local q = mce(a1 / y)
    qmin, qmax = triplediv(0, q, function(Q) return mab(a1 - y * Q) + mab(b1 + x * Q) end)
    local ca, cb, v = a1, b1, mab(a1) + mab(b1)
    for iq = qmin, qmax do
      local vv = mab(a1 - y * iq) + mab(b1 + x * iq)
      if vv < v then
        ca, cb, v = a1 - y * iq, b1 + x * iq, vv
      end
    end
    a1, b1 = ca, cb
  elseif 0 < b1 then
    local q = mce(b1 / x)
    qmin, qmax = triplediv(0, q, function(Q) return mab(a1 + y * Q) + mab(b1 - x * Q) end)
    local ca, cb, v = a1, b1, mab(a1) + mab(b1)
    for iq = qmin, qmax do
      local vv = mab(a1 + y * iq) + mab(b1 - x * iq)
      if vv < v then
        ca, cb, v = a1 + y * iq, b1 - x * iq, vv
      end
    end
    a1, b1 = ca, cb
  end
  -- print(a1, b1)
  z = 1

  local cnt = (mab(a1) + mab(b1)) * z
  if cnt * 2 - 1 <= 10000 then
    if 0 <= a1 and 0 <= b1 then
      io.write(string.rep("c", a1 * z))
      io.write(string.rep("w", b1 * z))
      io.write(string.rep("C", b1 * z - 1))
      io.write(string.rep("C", a1 * z))
      io.write("\n")
    elseif a1 <= 0 then
      io.write(string.rep("c", -a1 * z))
      io.write(string.rep("w", b1 * z))
      io.write(string.rep("C", b1 * z - 1))
      io.write(string.rep("W", -a1 * z))
      io.write("\n")
    else
      io.write(string.rep("w", -b1 * z))
      io.write(string.rep("c", a1 * z))
      io.write(string.rep("C", a1 * z - 1))
      io.write(string.rep("W", -b1 * z))
      io.write("\n")
    end
  else
    print("NO")
  end
end
0