結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
76,120 KB
testcase_01 AC 76 ms
71,276 KB
testcase_02 AC 75 ms
71,004 KB
testcase_03 AC 74 ms
71,348 KB
testcase_04 AC 75 ms
71,368 KB
testcase_05 AC 73 ms
71,392 KB
testcase_06 AC 88 ms
76,128 KB
testcase_07 AC 85 ms
76,200 KB
testcase_08 AC 89 ms
76,040 KB
testcase_09 AC 90 ms
76,044 KB
testcase_10 AC 88 ms
75,904 KB
testcase_11 AC 652 ms
76,148 KB
testcase_12 AC 597 ms
76,132 KB
testcase_13 AC 724 ms
76,196 KB
testcase_14 AC 551 ms
76,152 KB
testcase_15 AC 575 ms
76,220 KB
testcase_16 AC 427 ms
76,220 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**7,"Nが制約違反(N={})".format(N)
    assert 1<=K<=10**7,"Kが制約違反(N={})".format(K)
    assert 1<=H<=10**7,"Hが制約違反(N={})".format(H)
    assert 1<=Y<=10**14,"Yが制約違反(Y={})".format(Y)
    assert min(Y//N,Y//K,Y//H)<=10**7,"計算量が多すぎ(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