結果

問題 No.849 yuki国の分割統治
ユーザー betrue12betrue12
提出日時 2019-07-05 22:32:02
言語 Ruby
(3.2.2)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 663 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 11,228 KB
実行使用メモリ 23,620 KB
最終ジャッジ日時 2023-07-28 23:53:50
合計ジャッジ時間 9,141 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
15,016 KB
testcase_01 AC 85 ms
15,344 KB
testcase_02 AC 83 ms
15,260 KB
testcase_03 AC 83 ms
15,112 KB
testcase_04 AC 83 ms
15,288 KB
testcase_05 AC 83 ms
15,012 KB
testcase_06 AC 84 ms
15,076 KB
testcase_07 AC 328 ms
15,916 KB
testcase_08 AC 321 ms
19,568 KB
testcase_09 AC 330 ms
17,112 KB
testcase_10 AC 342 ms
16,432 KB
testcase_11 AC 302 ms
17,260 KB
testcase_12 AC 320 ms
15,864 KB
testcase_13 AC 335 ms
17,196 KB
testcase_14 AC 332 ms
16,868 KB
testcase_15 AC 301 ms
19,480 KB
testcase_16 AC 370 ms
20,744 KB
testcase_17 AC 323 ms
15,300 KB
testcase_18 AC 366 ms
23,132 KB
testcase_19 AC 341 ms
16,688 KB
testcase_20 AC 359 ms
20,204 KB
testcase_21 AC 305 ms
15,416 KB
testcase_22 AC 366 ms
20,712 KB
testcase_23 AC 375 ms
20,044 KB
testcase_24 AC 332 ms
16,392 KB
testcase_25 AC 361 ms
23,620 KB
testcase_26 AC 81 ms
15,016 KB
testcase_27 AC 81 ms
15,256 KB
testcase_28 AC 81 ms
15,088 KB
testcase_29 AC 82 ms
15,124 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def extgcd(a, b)
    if b == 0
        {x: 1, y: 0, gcd: a}
    else
        prev = extgcd(b, a % b)
        {
            x: prev[:y],
            y: prev[:x] - (a / b) * prev[:y],
            gcd: prev[:gcd]
        }
    end
end

a, b, c, d = gets.split.map(&:to_i)
swap = false
if a == 0 && c == 0
    swap = true
    a, b = b, a
    c, d = d, c
end
gx = a.gcd(c);
dy0 = (c*b-a*d).abs / gx

hash = extgcd(a/gx, c/gx)
dy1 = hash[:x]*b + hash[:y]*d

result = {}
N = gets.to_i
N.times do
    x, y = gets.split.map(&:to_i)
    x, y = y, x if swap
    x2 = x % gx
    y2 = y - (x-x2)/gx*dy1
    y2 %= dy0 if dy0 > 0
    result[[x2, y2]] = true
end
puts result.size
0