結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー GER_chenGER_chen
提出日時 2021-01-22 22:41:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,166 bytes
コンパイル時間 576 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 81,716 KB
最終ジャッジ日時 2023-08-27 20:44:15
合計ジャッジ時間 8,085 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
80,488 KB
testcase_01 AC 75 ms
75,840 KB
testcase_02 AC 74 ms
75,848 KB
testcase_03 AC 80 ms
76,116 KB
testcase_04 AC 81 ms
76,004 KB
testcase_05 AC 82 ms
76,072 KB
testcase_06 AC 110 ms
77,220 KB
testcase_07 AC 113 ms
77,332 KB
testcase_08 AC 120 ms
77,312 KB
testcase_09 AC 172 ms
77,360 KB
testcase_10 AC 118 ms
77,456 KB
testcase_11 AC 1,558 ms
77,592 KB
testcase_12 AC 1,290 ms
77,700 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#yuki-1358
from math import gcd
mod = 10**9+7
T = int(input())  #20以下

def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def num(a, b, s):
    g = gcd(a, b)
    if s%g:
        return 0
    if g > 1:
        a //= g
        b //= g
        s //= g
    ini = s*modinv(a,b)%b
    return (s//a-ini)//b+1

#testcases
for _ in range(T):
    N, K, H, Y = map(int, input().split())
    g = gcd(N, gcd(K, H))
    if Y%g:
        print(0)
    else:
        N, K, H, Y = N//g, K//g, H//g, Y//g
        g = gcd(K, H)
        if g > 1:
            ini = Y*modinv(N, g)%g
            ans = 0
            while N*ini <= Y:
                ans += num(K, H, Y-N*ini)
                ans %= mod
                ini += g
            print(ans)
        if g == 1:
            rev = modinv(K,H)
            ans = sum(((Y-i*N)//K-rev*(Y-i*N)%H)//H+1 for i in range(Y//N+1))
            print(ans)
0