結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー 👑 KazunKazun
提出日時 2020-09-25 01:16:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 720 ms / 2,000 ms
コード長 1,077 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 76,264 KB
最終ジャッジ日時 2023-09-30 18:14:15
合計ジャッジ時間 5,794 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
76,096 KB
testcase_01 AC 75 ms
71,552 KB
testcase_02 AC 77 ms
71,276 KB
testcase_03 AC 76 ms
71,048 KB
testcase_04 AC 73 ms
71,188 KB
testcase_05 AC 73 ms
71,048 KB
testcase_06 AC 88 ms
76,256 KB
testcase_07 AC 86 ms
76,184 KB
testcase_08 AC 88 ms
76,264 KB
testcase_09 AC 90 ms
76,188 KB
testcase_10 AC 87 ms
76,180 KB
testcase_11 AC 650 ms
76,004 KB
testcase_12 AC 596 ms
76,092 KB
testcase_13 AC 720 ms
76,172 KB
testcase_14 AC 548 ms
76,132 KB
testcase_15 AC 580 ms
76,028 KB
testcase_16 AC 433 ms
76,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def f(a, b):
    """
    ax + by = gcd(a,b) = d となる (x,y,d) を返す
    """
    if b == 0:
        return (1, 0, a)
    q, r = a // b, a % b
    x, y, d = f(b, r)
    s, t = y, x - q * y
    return s, t, d
#================================================
from math import gcd

T=int(input())
assert 1<=T<=100,"Tが制約違反(T={})".format(T)
for _ in range(T):
    N,K,H,Y=map(int,input().split())
    assert 1<=N<=10**6,"Nが制約違反(N={})".format(N)
    assert 1<=K<=10**6,"Kが制約違反(N={})".format(K)
    assert 1<=H<=10**6,"Hが制約違反(N={})".format(H)
    assert 1<=Y<=10**12,"Yが制約違反(Y={})".format(Y)
    assert min(Y//N,Y//K,Y//H)<=10**6,"計算量が多すぎ(N={},K={},H={},Y={})".format(N,K,H,Y)

    N,K,H=sorted([N,K,H],reverse=True)
    Mod=10**9+7
    Ans=0
    g=gcd(K,H)
    A,B=K//g,H//g
    p,q,_=f(A,B)

    for x in range(0,Y//N+1):
        beta=Y-N*x
        if beta%g:
            continue

        S=beta//g

        upper=S*q//A
        lower=(-S*p+(B-1))//B

        Ans+=upper-lower+1
        Ans%=Mod
    print(Ans)
0