結果

問題 No.2119 一般化百五減算
ユーザー maguroflymagurofly
提出日時 2022-11-05 00:42:05
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,054 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 11,160 KB
実行使用メモリ 21,012 KB
最終ジャッジ日時 2023-09-26 02:54:37
合計ジャッジ時間 4,491 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,276 KB
testcase_01 AC 80 ms
15,060 KB
testcase_02 AC 79 ms
15,144 KB
testcase_03 AC 81 ms
15,064 KB
testcase_04 AC 79 ms
15,300 KB
testcase_05 AC 80 ms
15,256 KB
testcase_06 AC 80 ms
15,248 KB
testcase_07 WA -
testcase_08 AC 79 ms
15,028 KB
testcase_09 AC 80 ms
15,172 KB
testcase_10 AC 80 ms
15,248 KB
testcase_11 WA -
testcase_12 AC 79 ms
15,300 KB
testcase_13 AC 79 ms
15,320 KB
testcase_14 AC 80 ms
15,296 KB
testcase_15 AC 80 ms
15,104 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 79 ms
15,320 KB
testcase_20 WA -
testcase_21 AC 197 ms
19,136 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 < lcm
        if ans > N
            ans = -1
            break
        end
    end
    
    if ans != -1
        puts ans % lcm
    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