結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー ayaoniayaoni
提出日時 2021-01-24 00:26:11
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 728 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 86,888 KB
実行使用メモリ 76,236 KB
最終ジャッジ日時 2023-08-30 10:37:42
合計ジャッジ時間 6,774 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
75,904 KB
testcase_01 AC 72 ms
71,176 KB
testcase_02 AC 72 ms
71,180 KB
testcase_03 AC 71 ms
71,208 KB
testcase_04 AC 71 ms
71,240 KB
testcase_05 AC 70 ms
71,236 KB
testcase_06 AC 92 ms
76,008 KB
testcase_07 AC 86 ms
76,108 KB
testcase_08 AC 89 ms
76,084 KB
testcase_09 AC 90 ms
76,116 KB
testcase_10 AC 89 ms
76,236 KB
testcase_11 AC 645 ms
75,832 KB
testcase_12 AC 596 ms
75,992 KB
testcase_13 AC 728 ms
76,208 KB
testcase_14 AC 540 ms
76,008 KB
testcase_15 AC 590 ms
76,072 KB
testcase_16 AC 587 ms
76,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


def ext_gcd(a, b):  # a*x+b*y == gcd(a,b) たる gcd(a,b),x,y
    if b > 0:
        d,x,y = ext_gcd(b,a % b)
        return d,y,x-(a//b)*y
    return a,1,0


T = I()
mod = 10**9+7
for _ in range(T):
    N,K,H,Y = MI()

    # Nを最大としてよい
    M = max(N,K,H)
    if M == K:
        N,K = K,N
    elif M == H:
        N,H = H,N

    g,a,b = ext_gcd(K,H)
    K //= g
    H //= g
    ans = 0
    for i in range(Y//N+1):
        Z = Y-N*i
        if Z % g != 0:
            continue
        Z //= g

        # K*j+H*k == Z の自然数解の個数
        c = a*Z
        d = b*Z
        ans += max(1+d//K-(-c+H-1)//H,0)
        ans %= mod
    print(ans)
0