結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー GER_chenGER_chen
提出日時 2021-01-22 22:51:05
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,004 ms / 2,000 ms
コード長 1,269 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 77,924 KB
最終ジャッジ日時 2023-08-27 21:14:16
合計ジャッジ時間 6,794 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
76,120 KB
testcase_01 AC 75 ms
71,244 KB
testcase_02 AC 75 ms
71,392 KB
testcase_03 AC 76 ms
71,440 KB
testcase_04 AC 73 ms
71,180 KB
testcase_05 AC 73 ms
71,640 KB
testcase_06 AC 113 ms
77,580 KB
testcase_07 AC 113 ms
77,652 KB
testcase_08 AC 110 ms
77,180 KB
testcase_09 AC 105 ms
77,192 KB
testcase_10 AC 109 ms
77,356 KB
testcase_11 AC 564 ms
77,640 KB
testcase_12 AC 652 ms
77,924 KB
testcase_13 AC 972 ms
77,636 KB
testcase_14 AC 1,004 ms
77,760 KB
testcase_15 AC 836 ms
77,420 KB
testcase_16 AC 438 ms
76,728 KB
権限があれば一括ダウンロードができます

ソースコード

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))%mod
            #ans = sum(num(K, H, Y-i*N) for i in range(Y//N+1))
        
        print(ans)
0