結果

問題 No.438 Cwwプログラミング入門
ユーザー te-sh
提出日時 2017-10-30 14:08:07
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,585 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 157,152 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-12 22:15:52
合計ジャッジ時間 4,902 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 98
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions

void main()
{
  auto rd = readln.split.to!(long[]), x = rd[0], y = rd[1], z = rd[2];
  writeln(calc(x, y, z));
}

auto calc(long x, long y, long z)
{
  if (z == 0) return "ccW";
  if (x == 0 && y == 0) return "NO";

  if (x == 0) {
    if (z % y) return "NO";
    auto g = z/y;
    if (g*2-1 > 10000) return "NO";
    return 'w'.repeat.take(g).chain('C'.repeat.take(g-1)).to!string;
  }
  if (y == 0) {
    if (z % x) return "NO";
    auto g = z/x;
    if (g*2-1 > 10000) return "NO";
    return 'c'.repeat.take(g).chain('C'.repeat.take(g-1)).to!string;
  }

  auto ma = 100000L, mb = 100000L;
  foreach (a; -5000L..5001L) {
    if ((z-a*x)%y) continue;
    auto b = (z-a*x)/y;

    if (a.abs*2+b.abs*2-1 > 10000) continue;

    if (a.abs*2+b.abs*2-1 < ma.abs*2+mb.abs*2-1) {
      ma = a;
      mb = b;
    }
  }

  if (ma.abs*2+mb.abs*2 > 10000) return "NO";

  if (ma == 0)
    return 'w'.repeat.take(mb).chain('C'.repeat.take(mb-1)).to!string;
  if (mb == 0)
    return 'c'.repeat.take(ma).chain('C'.repeat.take(ma-1)).to!string;

  if (ma < 0)
    return 'c'.repeat.take(-ma).chain('C'.repeat.take(-ma-1))
      .chain('w'.repeat.take(mb).chain('C'.repeat.take(mb-1))).to!string ~ "W";
  if (mb < 0)
    return 'w'.repeat.take(-mb).chain('C'.repeat.take(-mb-1))
      .chain('c'.repeat.take(ma).chain('C'.repeat.take(ma-1))).to!string ~ "W";

  return 'c'.repeat.take(ma).chain('C'.repeat.take(ma-1))
    .chain('w'.repeat.take(mb)).chain('C'.repeat.take(mb)).to!string;
}
0