Submission #28804079


Source Code Expand

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 = 30000000;
const bool replace_default_prng_implementation = false;

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 + 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);

Submission Info

Submission Time
Task D - Dance
User ssvb
Language D (GDC 9.2.1)
Score 400
Code Size 3778 Byte
Status AC
Exec Time 960 ms
Memory 7264 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 28
Set Name Test Cases
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
Case Name Status Exec Time Memory
000.txt AC 944 ms 7212 KiB
001.txt AC 928 ms 7124 KiB
002.txt AC 929 ms 7124 KiB
003.txt AC 925 ms 7072 KiB
004.txt AC 927 ms 6988 KiB
005.txt AC 922 ms 7040 KiB
006.txt AC 918 ms 7060 KiB
007.txt AC 928 ms 6992 KiB
008.txt AC 930 ms 7100 KiB
009.txt AC 928 ms 7264 KiB
010.txt AC 924 ms 7124 KiB
011.txt AC 925 ms 7072 KiB
012.txt AC 927 ms 7084 KiB
013.txt AC 930 ms 6988 KiB
014.txt AC 931 ms 7204 KiB
015.txt AC 928 ms 6988 KiB
016.txt AC 928 ms 7224 KiB
017.txt AC 938 ms 7096 KiB
018.txt AC 927 ms 7112 KiB
019.txt AC 927 ms 7076 KiB
020.txt AC 926 ms 7260 KiB
021.txt AC 923 ms 7068 KiB
022.txt AC 960 ms 7108 KiB
023.txt AC 929 ms 7184 KiB
024.txt AC 929 ms 7044 KiB
example0.txt AC 922 ms 7184 KiB
example1.txt AC 929 ms 7036 KiB
example2.txt AC 930 ms 7212 KiB