Submission #20534893


Source Code Expand

Copy
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)); }


Int mod(Int)(Int a, Int m) {
  if ((a %= m) < 0) a += m; return a;
}
Int gcd(Int)(Int a, Int b) { return (b != 0) ? gcd(b, a % b) : a; }
Int lcm(Int)(Int a, Int b) { return a / gcd(a, b) * b; }

Int gojo(Int)(Int a, Int b, out Int x, out Int y) {
  if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; }
  x = 1; y = 0; return a;
}

// as[i] T == bs[i] (mod ms[i])
// overflow suru!!!
Tuple!(Int, "b", Int, "m") modSol(Int)(Int[] as, Int[] bs, Int[] ms) {
  Int B = 0, M = 1;
  foreach (i; 0 .. ms.length) {
    const a = as[i] % ms[i], b = bs[i] % ms[i];
    const b0 = b - a * B;
    Int x, y;
    const g = gojo(a * M, ms[i], x, y);
    if (b0 % g != 0) { B = M = 0; break; }
    const m0 = ms[i] / g;
    B += M * ((x * ((b0 / g) % m0)) % m0);
    M *= m0;
    B %= M;
  }
  if (B < 0) B += M;
  return Tuple!(Int, "b", Int, "m")(B, M);
}

Tuple!(Int, "b", Int, "m") modSol(Int)(Int b0, Int m0, Int b1, Int m1) {
  if (m0 < m1) {
    swap(b0, b1);
    swap(m0, m1);
  }
  Int b, m, x, y;
  const g = gojo(m0, m1, x, y);
  if ((b1 - b0) % g == 0) {
    b = b0 + m0 * ((x * (((b1 - b0) / g) % (m1 / g))) % (m1 / g));
    m = m0 * (m1 / g);
    if ((b %= m) < 0) {
      b += m;
    }
  }
  return Tuple!(Int, "b", Int, "m")(b, m);
}


void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const X = readLong();
        const Y = readLong();
        const P = readLong();
        const Q = readLong();
        
        long ans = long.max;
        foreach (a; 0 .. Y) foreach (b; 0 .. Q) {
          const res = modSol(X + a, X + Y + X + Y, P + b, P + Q);
          if (res.m) {
            chmin(ans, res.b);
          }
        }
        
        if (ans >= long.max) {
          writeln("infinity");
        } else {
          writeln(ans);
        }
      }
    }
  } catch (EOFException e) {
  }
}

Submission Info

Submission Time
Task E - Oversleeping
User hos_lyric
Language D (LDC 1.20.1)
Score 500
Code Size 3157 Byte
Status AC
Exec Time 225 ms
Memory 3392 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 14
Set Name Test Cases
Sample 01_sample.txt
All 01_sample.txt, 02_hand.txt, 03_hand.txt, 04_small.txt, 05_small.txt, 06_small.txt, 07_small.txt, 08_small.txt, 09_inf.txt, 10_inf.txt, 11_inf.txt, 12_large.txt, 13_large.txt, 14_large.txt
Case Name Status Exec Time Memory
01_sample.txt AC 9 ms 3108 KB
02_hand.txt AC 5 ms 3276 KB
03_hand.txt AC 3 ms 3108 KB
04_small.txt AC 3 ms 3224 KB
05_small.txt AC 3 ms 3224 KB
06_small.txt AC 3 ms 3292 KB
07_small.txt AC 3 ms 3296 KB
08_small.txt AC 3 ms 3324 KB
09_inf.txt AC 3 ms 3228 KB
10_inf.txt AC 3 ms 3104 KB
11_inf.txt AC 3 ms 3376 KB
12_large.txt AC 108 ms 3392 KB
13_large.txt AC 157 ms 3296 KB
14_large.txt AC 225 ms 3172 KB