結果

問題 No.849 yuki国の分割統治
ユーザー betrue12betrue12
提出日時 2019-07-05 22:32:02
言語 Ruby
(3.3.0)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 663 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 22,400 KB
最終ジャッジ日時 2024-04-16 07:48:53
合計ジャッジ時間 9,214 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
12,032 KB
testcase_01 AC 94 ms
12,032 KB
testcase_02 AC 96 ms
12,032 KB
testcase_03 AC 95 ms
12,160 KB
testcase_04 AC 98 ms
12,160 KB
testcase_05 AC 95 ms
12,288 KB
testcase_06 AC 93 ms
12,160 KB
testcase_07 AC 338 ms
12,672 KB
testcase_08 AC 333 ms
16,768 KB
testcase_09 AC 340 ms
14,848 KB
testcase_10 AC 345 ms
12,928 KB
testcase_11 AC 315 ms
14,976 KB
testcase_12 AC 321 ms
12,672 KB
testcase_13 AC 353 ms
14,720 KB
testcase_14 AC 344 ms
14,208 KB
testcase_15 AC 318 ms
16,512 KB
testcase_16 AC 382 ms
18,816 KB
testcase_17 AC 318 ms
12,288 KB
testcase_18 AC 393 ms
21,632 KB
testcase_19 AC 350 ms
13,440 KB
testcase_20 AC 384 ms
17,152 KB
testcase_21 AC 313 ms
12,288 KB
testcase_22 AC 387 ms
18,816 KB
testcase_23 AC 377 ms
17,152 KB
testcase_24 AC 344 ms
13,056 KB
testcase_25 AC 387 ms
22,400 KB
testcase_26 AC 94 ms
12,288 KB
testcase_27 AC 94 ms
12,160 KB
testcase_28 AC 94 ms
12,288 KB
testcase_29 AC 96 ms
12,032 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