結果

問題 No.1203 お菓子ゲーム
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-08-28 22:13:29
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 778 ms / 2,000 ms
コード長 3,767 bytes
コンパイル時間 2,188 ms
コンパイル使用メモリ 107,220 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 09:22:34
合計ジャッジ時間 27,895 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 342 ms
4,376 KB
testcase_01 AC 465 ms
4,380 KB
testcase_02 AC 150 ms
4,376 KB
testcase_03 AC 567 ms
4,376 KB
testcase_04 AC 153 ms
4,376 KB
testcase_05 AC 655 ms
4,380 KB
testcase_06 AC 408 ms
4,376 KB
testcase_07 AC 609 ms
4,380 KB
testcase_08 AC 326 ms
4,380 KB
testcase_09 AC 303 ms
4,380 KB
testcase_10 AC 650 ms
4,376 KB
testcase_11 AC 153 ms
4,376 KB
testcase_12 AC 375 ms
4,376 KB
testcase_13 AC 272 ms
4,376 KB
testcase_14 AC 335 ms
4,380 KB
testcase_15 AC 705 ms
4,380 KB
testcase_16 AC 249 ms
4,376 KB
testcase_17 AC 536 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 60 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 732 ms
4,376 KB
testcase_31 AC 773 ms
4,376 KB
testcase_32 AC 730 ms
4,376 KB
testcase_33 AC 736 ms
4,376 KB
testcase_34 AC 725 ms
4,380 KB
testcase_35 AC 736 ms
4,376 KB
testcase_36 AC 732 ms
4,380 KB
testcase_37 AC 731 ms
4,380 KB
testcase_38 AC 732 ms
4,376 KB
testcase_39 AC 732 ms
4,376 KB
testcase_40 AC 775 ms
4,380 KB
testcase_41 AC 775 ms
4,380 KB
testcase_42 AC 777 ms
4,380 KB
testcase_43 AC 769 ms
4,380 KB
testcase_44 AC 773 ms
4,380 KB
testcase_45 AC 776 ms
4,380 KB
testcase_46 AC 777 ms
4,380 KB
testcase_47 AC 778 ms
4,380 KB
testcase_48 AC 773 ms
4,376 KB
testcase_49 AC 773 ms
4,376 KB
testcase_50 AC 2 ms
4,376 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 L = 10L^^8;

long calc(const(long) X, const(long) Y) {
  long ret;
  
  /*
    0 < j < i <= L
    j = 2 k
    X / Y = k / i
  */
  if (Y > 2 * X) {
    ret += L / Y;
  }
  
  /*
    0 < j < i <= L
    j = 2 k + 1
    X / Y = (2 k (k + 1)) / (i (2 k + 1))
    
    X > 0 ==> k > 0
  */
  void check(long d) {
    if (d >= 3 && d % 2 != 0) {
      const k = (d - 1) / 2;
      if ((2 * k * (k + 1)) % X == 0) {
        const q = (2 * k * (k + 1)) / X;
        if (Y / d <= L / q) {
          const i = (Y / d) * q;
          debug {
            writefln("X/Y = %s/%s, d = %s, k = %s, i = %s", X, Y, d, k, i);
          }
          if (2 * k + 1 < i && i <= L) {
            ++ret;
          }
        }
      }
    }
  }
  for (long d = 1; d^^2 <= Y; ++d) {
    if (Y % d == 0) {
      check(d);
      if (d != Y / d) {
        check(Y / d);
      }
    }
  }
  
  return ret;
}

void main() {
  debug {
    enum lim = 10;
    auto dp = new real[][](lim, lim);
    foreach (i; 0 .. lim) foreach (j; 0 .. lim) {
      if (i != j) {
        dp[i][j] = 0.0L;
        if (i > 0 && j > 0) {
          if (i > j) {
            dp[i][j] += (1.0L * (j - 1) / i) * dp[j - 1][j];
          } else {
            dp[i][j] += (1.0L * (i - 1) / j) * dp[i][i - 1];
            dp[i][j] += (1.0L * (j - (i - 1)) / j);
          }
        }
      }
    }
    foreach (i; 0 .. lim) {
      writeln(i, ": ", dp[i]);
      foreach (j; 1 .. i) {
        real prob = 1.0L * (j^^2 / 2) / (i * j);
        assert(abs(prob - dp[i][j]) <= 1e-12L);
      }
      if (i > 0) {
        foreach (j; i + 1 .. lim) {
          real prob = 1.0L - 1.0L * (i^^2 / 2) / (j * i);
          assert(abs(prob - dp[i][j]) <= 1e-12L);
        }
      }
    }
  }
  
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const X = readLong();
        const Y = readLong();
        
        long ans;
        ans += calc(X, Y);
        ans += calc(Y - X, Y);
        writeln(ans);
        
        debug {
          foreach (i; 1 .. 100) {
            foreach (j; 1 .. i) {
              if (j % 2 != 0) {
                long p = j^^2 / 2;
                long q = i * j;
                const g = gcd(p, q);
                p /= g;
                q /= g;
                if (p == X && q == Y) {
                  writeln("brt X/Y " , i, " ", j);
                }
                if (p == Y - X && q == Y) {
                  writeln("brt 1-X/Y " , i, " ", j);
                }
              }
            }
          }
        }
      }
    }
  } catch (EOFException e) {
  }
}
0