結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
76,412 KB
testcase_01 AC 79 ms
71,536 KB
testcase_02 AC 80 ms
71,628 KB
testcase_03 AC 76 ms
71,264 KB
testcase_04 AC 77 ms
71,480 KB
testcase_05 AC 76 ms
71,456 KB
testcase_06 AC 94 ms
76,360 KB
testcase_07 AC 93 ms
76,340 KB
testcase_08 AC 96 ms
76,120 KB
testcase_09 AC 94 ms
76,196 KB
testcase_10 AC 93 ms
76,620 KB
testcase_11 AC 657 ms
76,272 KB
testcase_12 AC 601 ms
76,268 KB
testcase_13 AC 726 ms
76,360 KB
testcase_14 AC 552 ms
76,212 KB
testcase_15 AC 584 ms
76,376 KB
testcase_16 AC 433 ms
76,436 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())
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