結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー rlangevinrlangevin
提出日時 2023-05-28 16:17:27
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 809 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 81,412 KB
最終ジャッジ日時 2023-08-27 13:51:11
合計ジャッジ時間 5,365 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
77,032 KB
testcase_01 AC 83 ms
75,544 KB
testcase_02 AC 86 ms
75,616 KB
testcase_03 AC 80 ms
75,484 KB
testcase_04 AC 80 ms
75,536 KB
testcase_05 AC 81 ms
75,564 KB
testcase_06 AC 148 ms
76,856 KB
testcase_07 AC 133 ms
77,064 KB
testcase_08 AC 141 ms
76,900 KB
testcase_09 AC 135 ms
77,192 KB
testcase_10 AC 133 ms
77,104 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from math import gcd

def extGCD(a, b):
    # ax + by = gcd(a, b)の解を求める
    x, y, u, v = 1, 0, 0, 1
    while b:
        k = a // b
        x -= k * u
        y -= k * v
        x, u = u, x
        y, v = v, y
        a, b = b, a % b
    return x, y

T = int(input())
mod = 10**9 + 7
for _ in range(T):
    N, K, H, Y = map(int, input().split())
    A = [N, K, H]
    A.sort()
    g = gcd(A[0], A[1])
    ans = 0
    for i in range(Y//A[-1] + 1):
        C = Y - i * A[-1]
        if C < 0:
            break
        if C % g != 0:
            continue
        c = C // g
        n, k = A[0]//g, A[1]//g
        p, q = extGCD(n, k)
        ma = c * q // n
        mi = (-c * p + k - 1)//k
        ans += max(0, (ma - mi) + 1)
        ans %= mod
    print(ans)
0