結果

問題 No.1315 渦巻洞穴
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-12 00:39:35
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 4,359 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 108,708 KB
実行使用メモリ 6,148 KB
最終ジャッジ日時 2023-09-04 11:29:31
合計ジャッジ時間 8,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 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,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 35 ms
4,384 KB
testcase_14 AC 28 ms
4,384 KB
testcase_15 AC 40 ms
4,376 KB
testcase_16 AC 38 ms
4,380 KB
testcase_17 AC 32 ms
4,376 KB
testcase_18 AC 39 ms
4,380 KB
testcase_19 AC 34 ms
4,504 KB
testcase_20 AC 40 ms
4,376 KB
testcase_21 AC 33 ms
4,376 KB
testcase_22 AC 39 ms
4,380 KB
testcase_23 AC 40 ms
4,384 KB
testcase_24 AC 38 ms
4,380 KB
testcase_25 AC 37 ms
4,380 KB
testcase_26 AC 40 ms
5,896 KB
testcase_27 AC 42 ms
4,384 KB
testcase_28 AC 37 ms
4,380 KB
testcase_29 AC 46 ms
4,380 KB
testcase_30 AC 45 ms
4,548 KB
testcase_31 AC 45 ms
4,376 KB
testcase_32 AC 52 ms
4,376 KB
testcase_33 AC 47 ms
4,544 KB
testcase_34 AC 43 ms
4,376 KB
testcase_35 AC 38 ms
4,388 KB
testcase_36 AC 40 ms
4,376 KB
testcase_37 AC 38 ms
4,384 KB
testcase_38 AC 33 ms
6,016 KB
testcase_39 AC 39 ms
4,384 KB
testcase_40 AC 41 ms
4,376 KB
testcase_41 AC 46 ms
4,380 KB
testcase_42 AC 44 ms
4,376 KB
testcase_43 AC 46 ms
4,380 KB
testcase_44 AC 46 ms
4,384 KB
testcase_45 AC 46 ms
5,900 KB
testcase_46 AC 46 ms
4,380 KB
testcase_47 AC 46 ms
4,380 KB
testcase_48 AC 45 ms
6,148 KB
testcase_49 AC 46 ms
4,380 KB
testcase_50 AC 45 ms
4,380 KB
testcase_51 AC 45 ms
4,384 KB
testcase_52 AC 45 ms
4,380 KB
testcase_53 AC 46 ms
5,256 KB
testcase_54 AC 46 ms
4,380 KB
testcase_55 AC 45 ms
4,380 KB
testcase_56 AC 45 ms
4,384 KB
testcase_57 AC 46 ms
4,380 KB
testcase_58 AC 45 ms
4,384 KB
testcase_59 AC 24 ms
4,380 KB
testcase_60 AC 29 ms
4,380 KB
testcase_61 AC 25 ms
4,380 KB
testcase_62 AC 18 ms
4,376 KB
testcase_63 AC 23 ms
4,380 KB
testcase_64 AC 29 ms
4,384 KB
testcase_65 AC 24 ms
4,376 KB
testcase_66 AC 18 ms
4,376 KB
testcase_67 AC 24 ms
4,376 KB
testcase_68 AC 29 ms
4,376 KB
testcase_69 AC 26 ms
4,380 KB
testcase_70 AC 19 ms
4,380 KB
testcase_71 AC 24 ms
4,376 KB
testcase_72 AC 30 ms
4,380 KB
testcase_73 AC 25 ms
4,384 KB
testcase_74 AC 19 ms
4,380 KB
testcase_75 AC 1 ms
4,380 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 26 ms
4,380 KB
testcase_78 AC 21 ms
4,384 KB
testcase_79 AC 41 ms
4,380 KB
testcase_80 AC 27 ms
4,380 KB
testcase_81 AC 27 ms
5,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum DX = [+1, 0, -1, 0];
enum DY = [0, +1, 0, -1];

long get(long x, long y) {
  const r = max(abs(x), abs(y));
  if (x == r && y > -r) {
    return (2 * r - 1)^^2 + (y - (-r));
  } else if (y == r) {
    return (2 * r - 1)^^2 + 2 * r + (r - x);
  } else if (x == -r) {
    return (2 * r - 1)^^2 + 4 * r + (r - y);
  } else {
    return (2 * r - 1)^^2 + 6 * r + (x - (-r));
  }
}

int[] solve(long start, long target) {
  if (start == 1) {
    if (target == 0) {
      // 1232121
      return [0, 1, 3, 2, 0, 2];
    } else {
      return [];
    }
  }
  long x, y;
  for (long r = 0; ; ++r) {
    if (start <= (2 * r - 1)^^2 + 2 * r) {
      x = r;
      y = (-r) + (start - (2 * r - 1)^^2);
      break;
    } else if (start <= (2 * r - 1)^^2 + 4 * r) {
      x = r - (start - (2 * r - 1)^^2 - 2 * r);
      y = r;
      break;
    } else if (start <= (2 * r - 1)^^2 + 6 * r) {
      x = -r;
      y = r - (start - (2 * r - 1)^^2 - 4 * r);
      break;
    } else if (start <= (2 * r - 1)^^2 + 8 * r) {
      x = (-r) + (start - (2 * r - 1)^^2 - 6 * r);
      y = -r;
      break;
    }
  }
  debug {
    writeln("solve ", start, " ", target, ": ", x, " ", y);
  }
  assert(get(x, y) == start);
  int[] ret;
  long sum = target ^ start;
  const x0 = x, y0 = y;
  for (; get(x, y) > 1; ) {
    int dir;
    long xx, yy;
    if (sum <= 1) {
      if (get(x, y) == 3) {
        if (sum == 0) {
          // 23232121
          ret ~= [3, 1, 3, 1, 3, 2, 0, 2];
        } else {
          // 232121
          ret ~= [3, 1, 3, 2, 0, 2];
        }
        sum = 0;
        break;
      } else {
        for (dir = 0; dir < 4; ++dir) {
          xx = x + DX[dir], yy = y + DY[dir];
          if (max(abs(x), abs(y)) > max(abs(xx), abs(yy))) {
            goto found;
          }
        }
        for (dir = 0; dir < 4; ++dir) {
          xx = x + DX[dir], yy = y + DY[dir];
          if (max(abs(x), abs(y)) == max(abs(xx), abs(yy))) {
            if ((get(x, y) ^ get(xx, yy)) != 1) {
              goto found;
            }
          }
        }
        assert(false);
      }
    } else {
      for (dir = 0; dir < 4; ++dir) {
        xx = x + DX[dir], yy = y + DY[dir];
        if ((get(x, y) ^ get(xx, yy)) == 1) {
          goto found;
        }
      }
      assert(false);
    }
   found:
    ret ~= dir;
    sum ^= get(xx, yy);
    x = xx;
    y = yy;
  }
  if (sum != 0) {
    foreach (dir; 0 .. 4) {
      if ((get(x0, y0) ^ get(x0 + DX[dir], y0 + DY[dir])) == 1) {
        ret = [dir, dir ^ 2] ~ ret;
        break;
      }
    }
  }
  return ret;
}

void main() {
  debug {
    foreach_reverse (y; -4 .. +4 + 1) {
      foreach (x; -4 .. +4 + 1) {
        write(get(x, y), " ");
      }
      writeln;
    }
  }
  
  try {
    for (; ; ) {
      const S = readLong();
      const T = readLong();
      
      const res0 = solve(S, 0);
      const res1 = solve(T, 1);
      int[] ans;
      foreach (dir; res0) {
        ans ~= dir;
      }
      foreach_reverse (dir; res1) {
        ans ~= dir ^ 2;
      }
      
      writeln(0);
      writeln(ans.length);
      foreach (dir; ans) {
        write("RULD"[dir]);
      }
      writeln;
    }
  } catch (EOFException e) {
  }
}
0