結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー brthyyjp
提出日時 2021-01-23 03:25:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 797 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 125,056 KB
最終ジャッジ日時 2024-12-29 13:29:19
合計ジャッジ時間 14,006 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1 WA * 13 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = sys.stdin.buffer.readline

t = int(input())
mod = 10**9+7

def extgcd(a, b):
    # ax+by = gcd(x, y)の整数解を求める
    # x, y, gcd(a, b)を返す
    if b == 0:
        return 1, 0, a
    q, r = divmod(a, b)
    x, y, d = extgcd(b, r)
    s, t = y, x-q*y
    return s, t, d

import math

for _ in range(t):
    n, k, h, y = map(int, input().split())
    G = math.gcd(n, math.gcd(k, h))
    if y%G:
        print(0)
        continue
    n, k, h, y = n//G, k//G, h//G, y//G
    a0, b0, g = extgcd(n, k)
    #print(a0, b0)
    ans = 0
    for c in range(y//h+1):
        if (y-h*c)%g == 0:
            p = (y-h*c)//g
            cnt = max((p*b0)//n-(-p*a0+k-1)//k+1, 0)
        else:
            cnt = 0
        ans += cnt
        ans %= mod
    print(ans)
0