結果
問題 | No.261 ぐるぐるぐるぐる!あみだくじ! |
ユーザー | rpy3cpp |
提出日時 | 2015-09-19 03:40:17 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 33 ms / 5,000 ms |
コード長 | 1,741 bytes |
コンパイル時間 | 77 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-07-19 07:51:51 |
合計ジャッジ時間 | 2,493 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 27 ms
10,752 KB |
testcase_01 | AC | 26 ms
10,880 KB |
testcase_02 | AC | 26 ms
10,752 KB |
testcase_03 | AC | 28 ms
10,752 KB |
testcase_04 | AC | 29 ms
10,880 KB |
testcase_05 | AC | 29 ms
10,624 KB |
testcase_06 | AC | 28 ms
10,752 KB |
testcase_07 | AC | 28 ms
10,880 KB |
testcase_08 | AC | 29 ms
11,008 KB |
testcase_09 | AC | 28 ms
10,880 KB |
testcase_10 | AC | 30 ms
10,752 KB |
testcase_11 | AC | 31 ms
10,624 KB |
testcase_12 | AC | 30 ms
10,752 KB |
testcase_13 | AC | 30 ms
10,752 KB |
testcase_14 | AC | 32 ms
10,880 KB |
testcase_15 | AC | 33 ms
10,624 KB |
testcase_16 | AC | 32 ms
10,880 KB |
testcase_17 | AC | 30 ms
10,880 KB |
testcase_18 | AC | 31 ms
10,752 KB |
testcase_19 | AC | 32 ms
10,752 KB |
testcase_20 | AC | 31 ms
10,752 KB |
testcase_21 | AC | 29 ms
10,880 KB |
testcase_22 | AC | 29 ms
10,624 KB |
testcase_23 | AC | 31 ms
10,752 KB |
testcase_24 | AC | 30 ms
10,624 KB |
testcase_25 | AC | 31 ms
10,880 KB |
testcase_26 | AC | 32 ms
10,880 KB |
testcase_27 | AC | 30 ms
11,008 KB |
testcase_28 | AC | 31 ms
10,880 KB |
testcase_29 | AC | 30 ms
10,880 KB |
testcase_30 | AC | 30 ms
10,880 KB |
testcase_31 | AC | 30 ms
10,880 KB |
testcase_32 | AC | 30 ms
10,752 KB |
testcase_33 | AC | 31 ms
10,752 KB |
testcase_34 | AC | 31 ms
10,752 KB |
testcase_35 | AC | 30 ms
10,880 KB |
testcase_36 | AC | 31 ms
10,880 KB |
testcase_37 | AC | 28 ms
10,880 KB |
testcase_38 | AC | 29 ms
10,752 KB |
testcase_39 | AC | 28 ms
10,880 KB |
ソースコード
def read_data(): N = int(input()) K = int(input()) amida = list(range(N)) for k in range(K): x, y = map(int, input().split()) amida[x-1], amida[y-1] = amida[y-1], amida[x-1] Q = int(input()) As = [] for q in range(Q): a = list(map(int, input().split())) As.append(a) return N, amida, Q, As def solve(N, amida, Q, As): cycles = [0] * N pos = [[-1] * N for n in range(N)] for idx in range(N): find_cycle(N, amida, idx, cycles, pos[idx]) shuffle = [chinese_remainder(a, cycles, pos) for a in As] return shuffle def find_cycle(N, amida, idx, cycles, mypos): mypos[idx] = 0 a = amida[idx] count = 1 while a != idx: mypos[a] = count a = amida[a] count += 1 cycles[idx] = count def chinese_remainder(a, cycles, pos): xy = [] for ai, cycle, mypos in zip(a, cycles, pos): x = mypos[ai - 1] if x == -1: return -1 xy.append((x, cycle)) return CRT(xy) def gcd_ext(m, n): x, y, u, v = 1, 0, 0, 1 while n: k, m = divmod(m, n) m, n = n, m x, y, u, v = u, v, x - u * k, y - v * k return m, x, y def solve2(x1, y1, x2, y2): gcd, a, b = gcd_ext(y1, y2) lcm = y1 * y2 // gcd k, r = divmod(x1 - x2, gcd) if r != 0: return -1, lcm z = (x1 - k * a * y1) % lcm return z, lcm def CRT(xy): x0, y0 = xy[0] if len(xy) == 1: return x0 if x0 else y0 for x1, y1 in xy[1:]: x0, y0 = solve2(x0, y0, x1, y1) if x0 == -1: return -1 return x0 if x0 else y0 N, amida, Q, As = read_data() shuffles = solve(N, amida, Q, As) for shuffle in shuffles: print(shuffle)