結果
問題 | No.261 ぐるぐるぐるぐる!あみだくじ! |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-01-24 15:25:47 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 4,129 bytes |
コンパイル時間 | 1,101 ms |
コンパイル使用メモリ | 133,164 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-13 03:39:16 |
合計ジャッジ時間 | 2,435 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 1 ms
6,944 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,940 KB |
testcase_14 | AC | 2 ms
6,944 KB |
testcase_15 | AC | 2 ms
6,944 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 2 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,940 KB |
testcase_20 | AC | 2 ms
6,940 KB |
testcase_21 | AC | 2 ms
6,940 KB |
testcase_22 | AC | 2 ms
6,944 KB |
testcase_23 | AC | 2 ms
6,940 KB |
testcase_24 | AC | 2 ms
6,944 KB |
testcase_25 | AC | 2 ms
6,944 KB |
testcase_26 | AC | 2 ms
6,940 KB |
testcase_27 | AC | 2 ms
6,940 KB |
testcase_28 | AC | 3 ms
6,944 KB |
testcase_29 | AC | 2 ms
6,940 KB |
testcase_30 | AC | 2 ms
6,944 KB |
testcase_31 | AC | 2 ms
6,940 KB |
testcase_32 | AC | 2 ms
6,940 KB |
testcase_33 | AC | 2 ms
6,940 KB |
testcase_34 | AC | 3 ms
6,940 KB |
testcase_35 | AC | 2 ms
6,944 KB |
testcase_36 | AC | 2 ms
6,940 KB |
testcase_37 | AC | 1 ms
6,940 KB |
testcase_38 | AC | 1 ms
6,944 KB |
testcase_39 | AC | 2 ms
6,944 KB |
コンパイルメッセージ
Main.d(90): Deprecation: foreach: loop index implicitly converted from `size_t` to `int` Main.d(91): Deprecation: foreach: loop index implicitly converted from `size_t` to `int` Main.d(103): Deprecation: foreach: loop index implicitly converted from `size_t` to `int` Main.d(106): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
ソースコード
import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T 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]) 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); } int N, K; int[] X, Y; int Q; int[][] A; void main() { try { for (; ; ) { N = readInt(); K = readInt(); X = new int[K]; Y = new int[K]; foreach (k; 0 .. K) { X[k] = readInt() - 1; Y[k] = readInt() - 1; } Q = readInt(); A = new int[][](Q, N); foreach (q; 0 .. Q) { foreach (u; 0 .. N) { A[q][u] = readInt() - 1; } } auto P = iota(N).array; foreach (k; 0 .. K) { swap(P[X[k]], P[Y[k]]); } debug { writeln("P = ", P); } auto vis = new bool[N]; int[][] cycles; foreach (u; 0 .. N) { if (!vis[u]) { cycles ~= [[]]; for (int v = u; !vis[v]; v = P[v]) { cycles[$ - 1] ~= v; vis[v] = true; } } } auto es = new int[N]; auto fs = new int[N]; foreach (int e, cycle; cycles) { foreach (int f, u; cycle) { es[u] = e; fs[u] = f; } } debug { writeln("cycles = ", cycles); } foreach (q; 0 .. Q) { long ans; long[] as, bs, ms; foreach (int e, cycle; cycles) { const len = cast(int)(cycle.length); int[] df; foreach (int f, u; cycle) { const ee = es[A[q][u]]; const ff = fs[A[q][u]]; if (e != ee) { ans = -1; goto failed; } df ~= (ff - f + len) % len; } df = df.sort.uniq.array; if (df.length != 1) { ans = -1; goto failed; } as ~= 1; bs ~= df[0]; ms ~= len; } { debug { writeln("bs = ", bs); writeln("ms = ", ms); } auto res = modSol(as, bs, ms); if (res.m == 0) { ans = -1; goto failed; } ans = res.b; if (ans == 0) { ans = res.m; } } failed: writeln(ans); } } } catch (EOFException e) { } }