提出 #28804747


ソースコード 拡げる

import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

const int  max_n = 16;
const int  number_of_trials = 300000000;
const bool replace_default_prng_implementation = true;

void main()
{
  static if (replace_default_prng_implementation) {
    // Use JSF32 instead of Mersenne Twister
    auto rng = Jsf32(unpredictableSeed);
    // Replace uniform function
    uint uniform(const uint lb, const uint ub) { return lb + biased_bounded_rand(ub - lb, rng); }
  }

  // Process input
  auto n = readln.strip.to!uint;
  assert(n <= max_n);
  int[max_n * 2][max_n * 2] a;
  foreach (row ; 0 .. 2 * n - 1) {
    foreach (col ; 2 * n - (2 * n - 1 - row) .. 2 * n) {
      readf!" %d "(a[row][col]);
      a[col][row] = a[row][col];
    }
  }

  auto p = iota(2 * n).array;

  // Calculate XOR value for the initial permutation
  int cur_xor = 0;
  foreach (k ; 0 .. p.length / 2) {
    auto i = p[k * 2 + 0];
    auto j = p[k * 2 + 1];
    cur_xor ^= a[i][j];
  }
  int best_xor = cur_xor;

  // Now try random permutations
  foreach (trial ; 0 .. number_of_trials) {
    // Pick two random indexes to swap
    auto idx1 = uniform(0, 2 * n);
    auto idx2 = uniform(0, 2 * n - 1);
    idx2 += (idx2 >= idx1) ? 1 : 0;

    // Undo XOR, swap and re-apply XOR
    cur_xor ^= a[p[idx1]][p[idx1 ^ 1]];
    cur_xor ^= a[p[idx2]][p[idx2 ^ 1]];
    swap(p[idx1], p[idx2]);
    cur_xor ^= a[p[idx1]][p[idx1 ^ 1]];
    cur_xor ^= a[p[idx2]][p[idx2 ^ 1]];

    best_xor = max(best_xor, cur_xor);
  }
  writeln(best_xor);
}

// From: https://www.pcg-random.org/posts/bounded-rands.html
uint bounded_rand(UniformRNG)(const uint range, ref UniformRNG rng)
{
  ulong x = std.random.uniform!uint(rng);
  ulong m = x * range;
  uint  l = cast(uint)m;
  if (l < range) {
    uint t = -range;
    if (t >= range) {
      t -= range;
      if (t >= range)
        t %= range;
    }
    while (l < t) {
      x = std.random.uniform!uint(rng);
      m = x * range;
      l = cast(uint)m;
    }
  }
  return m >> 32;
}

// From: https://www.pcg-random.org/posts/bounded-rands.html
uint biased_bounded_rand(UniformRNG)(const uint range, ref UniformRNG rng)
{
  return (cast(ulong)std.random.uniform!uint(rng) * range) >> 32;
}

// From: https://burtleburtle.net/bob/rand/smallprng.html
struct JsfEngine(UIntType)
{
  public:
    /// Mark this as a Rng
    enum bool isUniformRandom = true;
    /// Always `false` (random generators are infinite ranges).
    enum empty = false;
    /// Smallest generated value.
    enum UIntType min = UIntType.min;
    /// Largest generated value.
    enum UIntType max = UIntType.max;

  private:
    struct ranctx { UIntType a; UIntType b; UIntType c; UIntType d; }
    ranctx ctx;

    UIntType rot(UIntType x, int k) { return (((x)<<(k))|((x)>>(32-(k)))); }

    UIntType ranval(ref ranctx x)
    {
      UIntType e = x.a - rot(x.b, 27);
      x.a = x.b ^ rot(x.c, 17);
      x.b = x.c + x.d;
      x.c = x.d + e;
      x.d = e + x.a;
      return x.d;
    }

    void raninit(ref ranctx x, UIntType seed)
    {
      x.a = 0xf1ea5eed; x.b = x.c = x.d = seed;
      for (int i = 0; i < 21; ++i)
        ranval(x);
    }

  public:

    this()(UIntType x0) @safe pure nothrow @nogc { seed(x0); }

    void seed()(UIntType x0) @safe pure nothrow @nogc { raninit(ctx, x0); }

    @property
    UIntType front() const @safe pure nothrow @nogc { return ctx.d; }

    void popFront() @safe pure nothrow @nogc { ranval(ctx); }

    @property
    typeof(this) save() const @safe pure nothrow @nogc { return this; }
}

alias Jsf32 = JsfEngine!(uint);

提出情報

提出日時
問題 D - Dance
ユーザ ssvb
言語 D (GDC 9.2.1)
得点 400
コード長 3785 Byte
結果 AC
実行時間 1687 ms
メモリ 7176 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 28
セット名 テストケース
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, example0.txt, example1.txt, example2.txt
ケース名 結果 実行時間 メモリ
000.txt AC 1628 ms 7140 KiB
001.txt AC 1615 ms 6972 KiB
002.txt AC 1686 ms 7012 KiB
003.txt AC 1684 ms 7144 KiB
004.txt AC 1687 ms 7056 KiB
005.txt AC 1646 ms 7144 KiB
006.txt AC 1667 ms 7012 KiB
007.txt AC 1678 ms 7016 KiB
008.txt AC 1679 ms 7048 KiB
009.txt AC 1682 ms 7148 KiB
010.txt AC 1623 ms 6904 KiB
011.txt AC 1682 ms 6876 KiB
012.txt AC 1676 ms 7024 KiB
013.txt AC 1651 ms 7112 KiB
014.txt AC 1677 ms 7052 KiB
015.txt AC 1684 ms 7172 KiB
016.txt AC 1685 ms 7020 KiB
017.txt AC 1685 ms 6956 KiB
018.txt AC 1684 ms 7012 KiB
019.txt AC 1684 ms 6976 KiB
020.txt AC 1684 ms 7168 KiB
021.txt AC 1684 ms 6996 KiB
022.txt AC 1684 ms 6940 KiB
023.txt AC 1685 ms 7176 KiB
024.txt AC 1687 ms 6944 KiB
example0.txt AC 1605 ms 7040 KiB
example1.txt AC 1614 ms 7020 KiB
example2.txt AC 1664 ms 7048 KiB