結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー GER_chenGER_chen
提出日時 2021-01-22 22:48:08
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,267 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 86,880 KB
実行使用メモリ 81,796 KB
最終ジャッジ日時 2023-08-27 21:05:50
合計ジャッジ時間 5,366 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 101 ms
81,136 KB
testcase_01 AC 76 ms
75,536 KB
testcase_02 AC 80 ms
75,440 KB
testcase_03 AC 74 ms
75,356 KB
testcase_04 AC 77 ms
75,528 KB
testcase_05 AC 73 ms
75,720 KB
testcase_06 AC 154 ms
77,480 KB
testcase_07 AC 140 ms
77,492 KB
testcase_08 AC 143 ms
78,092 KB
testcase_09 AC 136 ms
77,580 KB
testcase_10 AC 144 ms
77,768 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
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())
    N, K, H = sorted([N, K, H])[::-1]
    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
                
        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))
            ans = sum(num(K, H, Y-i*N) for i in range(Y//N+1))
        
        print(ans)
0