結果

問題 No.1840 Random Painting
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-02-11 22:39:36
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 3,440 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 109,316 KB
実行使用メモリ 20,768 KB
最終ジャッジ日時 2023-09-04 15:56:34
合計ジャッジ時間 3,123 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 28 ms
8,068 KB
testcase_11 AC 50 ms
14,076 KB
testcase_12 AC 35 ms
9,692 KB
testcase_13 AC 13 ms
4,684 KB
testcase_14 AC 58 ms
20,768 KB
testcase_15 AC 48 ms
12,412 KB
testcase_16 AC 51 ms
13,300 KB
testcase_17 AC 27 ms
6,636 KB
testcase_18 AC 60 ms
20,564 KB
testcase_19 AC 59 ms
20,176 KB
testcase_20 AC 59 ms
19,744 KB
testcase_21 AC 58 ms
18,964 KB
testcase_22 AC 58 ms
19,328 KB
testcase_23 AC 59 ms
20,520 KB
testcase_24 AC 12 ms
5,608 KB
testcase_25 AC 13 ms
4,616 KB
testcase_26 AC 12 ms
4,660 KB
testcase_27 AC 13 ms
4,680 KB
testcase_28 AC 13 ms
4,716 KB
testcase_29 AC 13 ms
4,672 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,384 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)); }


// n * (n + 1)
// modifies a
real[] solveLinearEq(real[][] a) {
  const n = cast(int)(a.length);
  foreach (h; 0 .. n) {
    int im = h;
    foreach (i; h + 1 .. n) {
      if (abs(a[im][h]) < abs(a[i][h])) {
        im = i;
      }
    }
    swap(a[h], a[im]);
    a[h][] /= a[h][h];
    foreach (i; h + 1 .. n) {
      foreach_reverse (j; h .. n + 1) {
        a[i][j] -= a[i][h] * a[h][j];
      }
    }
  }
  auto x = new real[n];
  foreach_reverse (i; 0 .. n) {
    x[i] = a[i][n];
    foreach (j; i + 1 .. n) {
      x[i] -= a[i][j] * x[j];
    }
  }
  return x;
}


// https://oeis.org/A225233
long f(long x, long y) {
  const n = x + y;
  return (2 * n - x) * x;
}

void main() {
  debug {{
    // E[time | reach 0]
    foreach (n; 1 .. 10 + 1) {
      auto a = new real[][](n, n + 1);
      foreach (x; 0 .. n) {
        a[x][] = 0.0L;
        a[x][x] += 1.0L;
        if (x > 0) {
          a[x][n] = 1.0L * (n - x) / n;
          {
            a[x][x - 1] -= 0.5L;
          }
          if (x + 1 < n) {
            a[x][x + 1] -= 0.5L;
          }
        }
      }
      auto es = a.solveLinearEq;
      foreach (x; 0 .. n) {
        es[x] /= (1.0L * (n - x) / n);
      }
      es[] *= 3.0L;
      writeln(es);
      foreach (x; 0 .. n) {
        assert(abs(f(x, n - x) - es[x]) <= 1e-6L);
      }
    }
  }}
  
  try {
    for (; ; ) {
      const N = readInt;
      const S = readToken;
      
      int[] xs;
      foreach (i; 1 .. N) if (S[i] == '0') {
        xs ~= i;
      }
      const len = cast(int)(xs.length);
      xs = xs ~ [0] ~ xs;
      xs[0 .. len] -= N;
      debug {
        writeln("xs = ", xs);
      }
      
      long ans;
      foreach (j; 0 .. len + 1) {
        const long l = -xs[j];
        const long r = xs[j + len];
        // 0 -> -l -> +r
        // 0 -> +r -> -l
        /*
        long sum;
        sum += r * (N - l - r) * (f(l, r) + f(l + r, N - l - r));
        sum += l * (N - l - r) * (f(r, l) + f(l + r, N - l - r));
        assert(sum % (l + r) == 0);
        sum /= (l + r);
        ans += sum;
        */
        ans += (l + r - N) * (l * l - l * r + r * r - 2 * l * N - 2 * r * N);
      }
      assert(ans % (3 * N) == 0);
      ans /= (3 * N);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0