結果

問題 No.2119 一般化百五減算
ユーザー maguroflymagurofly
提出日時 2022-11-05 00:42:44
言語 Ruby
(3.2.2)
結果
TLE  
実行時間 -
コード長 1,035 bytes
コンパイル時間 567 ms
コンパイル使用メモリ 11,224 KB
実行使用メモリ 145,656 KB
最終ジャッジ日時 2023-09-26 02:55:18
合計ジャッジ時間 11,413 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,252 KB
testcase_01 AC 81 ms
15,140 KB
testcase_02 AC 82 ms
15,260 KB
testcase_03 AC 80 ms
15,124 KB
testcase_04 AC 81 ms
15,180 KB
testcase_05 AC 81 ms
15,128 KB
testcase_06 AC 81 ms
15,128 KB
testcase_07 AC 82 ms
15,280 KB
testcase_08 AC 81 ms
15,272 KB
testcase_09 AC 80 ms
15,200 KB
testcase_10 AC 81 ms
15,024 KB
testcase_11 AC 80 ms
15,264 KB
testcase_12 AC 80 ms
15,132 KB
testcase_13 AC 81 ms
15,080 KB
testcase_14 AC 81 ms
15,036 KB
testcase_15 AC 81 ms
15,012 KB
testcase_16 AC 80 ms
15,124 KB
testcase_17 AC 81 ms
15,024 KB
testcase_18 AC 82 ms
15,168 KB
testcase_19 AC 82 ms
15,272 KB
testcase_20 TLE -
testcase_21 AC 202 ms
19,028 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

main = -> {
    N = gets.to_i
    M = gets.to_i
    pairs = Array.new(M) { gets.split.map(&:to_i) }
    
    ans = 0
    lcm = 1
    pairs.each do |b, c|
        c %= b
        if lcm < b
            lcm, b = b, lcm
            ans, c = c, ans
        end
        if lcm % b == 0
            if ans % b != c
                ans = -1
                break
            end
            next
        end
        g, i = inv_gcd(lcm, b)
        u1 = b / g
        if (c - ans) % g != 0
            ans = -1
            break
        end
        x = (c - ans) / g % u1 * i % u1
        ans += x * lcm
        lcm *= u1
        ans %= lcm
        if ans > N
            ans = -1
            break
        end
    end
    
    if ans != -1
        puts ans
    else
        puts "NaN"
    end
}

def inv_gcd(a, b)
    a %= b
    return [b, 0] if a == 0
    s, t, x, y = b, a, 0, 1
    while t != 0
        u = s / t
        s -= t * u
        x -= y * u
        s, t, x, y = t, s, y, x
    end
    x += b / s if x < 0
    [s, x]
end

main.call
0