Submission #18874373


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


void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readLong();
        const S = readLong();
        const K = readLong();
        
        long[] as = [K];
        long[] bs = [(N - S) % N];
        long[] ms = [N];
        const res = modSol(as, bs, ms);
        const ans = res.m ? res.b : -1;
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

Submission Info

Submission Time
Task E - Throne
User hos_lyric
Language D (LDC 1.20.1)
Score 500
Code Size 2554 Byte
Status AC
Exec Time 9 ms
Memory 3396 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 32
Set Name Test Cases
Sample sample_01.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, sample_01.txt
Case Name Status Exec Time Memory
hand_01.txt AC 7 ms 3396 KB
random_01.txt AC 4 ms 3308 KB
random_02.txt AC 4 ms 3296 KB
random_03.txt AC 6 ms 3336 KB
random_04.txt AC 4 ms 3276 KB
random_05.txt AC 4 ms 3292 KB
random_06.txt AC 4 ms 3340 KB
random_07.txt AC 4 ms 3344 KB
random_08.txt AC 5 ms 3328 KB
random_09.txt AC 4 ms 3380 KB
random_10.txt AC 4 ms 3372 KB
random_11.txt AC 3 ms 3228 KB
random_12.txt AC 4 ms 3100 KB
random_13.txt AC 4 ms 3280 KB
random_14.txt AC 4 ms 3220 KB
random_15.txt AC 4 ms 3224 KB
random_16.txt AC 4 ms 3360 KB
random_17.txt AC 4 ms 3308 KB
random_18.txt AC 4 ms 3380 KB
random_19.txt AC 4 ms 3336 KB
random_20.txt AC 4 ms 3344 KB
random_21.txt AC 4 ms 3292 KB
random_22.txt AC 6 ms 3168 KB
random_23.txt AC 5 ms 3108 KB
random_24.txt AC 4 ms 3336 KB
random_25.txt AC 4 ms 3224 KB
random_26.txt AC 4 ms 3192 KB
random_27.txt AC 4 ms 3340 KB
random_28.txt AC 4 ms 3184 KB
random_29.txt AC 9 ms 3328 KB
random_30.txt AC 5 ms 3164 KB
sample_01.txt AC 4 ms 3368 KB