結果

問題 No.2280 FizzBuzz Difference
ユーザー sotanishysotanishy
提出日時 2023-04-21 23:31:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,622 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 78,732 KB
最終ジャッジ日時 2024-04-24 09:26:56
合計ジャッジ時間 1,733 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
52,096 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
import sys
input = sys.stdin.readline


def floor_sum(n, m, a, b):
    s = 0
    if a >= m:
        s += (a // m) * n * (n - 1) // 2
        a %= m
    if b >= m:
        s += (b // m) * n
        b %= m
    y = (a * n + b) // m
    if y == 0:
        return s
    x = (m * y - b + a - 1) // a
    s += (n - x) * y + floor_sum(y, a, m, a * x - m * y + b)
    return s


def extgcd(a, b):
    s, sx, sy, t, tx, ty = a, 1, 0, b, 0, 1
    while t:
        q = s // t
        s -= t * q
        s, t = t, s
        sx -= tx * q
        sx, tx = tx, sx
        sy -= ty * q
        sy, ty = ty, sy
    return sx, sy


def calc(A, B, K, M):
    # find the number of (x,y)
    # such that Ax+K=By
    # and 1 <= x <= M/A
    # and 1 <= y <= M/B

    g = gcd(A, B)
    if K % g != 0:
        return 0
    A //= g
    B //= g
    K //= g

    x0, y0 = extgcd(-A, B)
    x0 *= K
    y0 *= K

    # x = x0 + Bt
    # y= y0 + At

    tmin = max((1-x0+B-1)//B, (1-y0+A-1)//A)
    tmax = min((M//A-x0)//B, (M//B-y0)//A)
    return max(0, tmax-tmin+1)


T = int(input())
for _ in range(T):
    M, A, B, K = map(int, input().split())

    if K > A:
        print(0)
        continue

    if B % A == 0:
        if K == A:
            print(M//A-1)
        else:
            print(0)
        continue

    if A == K:
        C = M//A-1
        lcm = A*B//gcd(A, B)
        cntBnotA = M//B - M//lcm
        cnt_all = M//A + cntBnotA
        ans = cnt_all - 1 - 2*cntBnotA
        if M % B < M % lcm:
            ans += 1
        print(ans)
        continue

    ans = calc(A, B, K, M) + calc(B, A, K, M)
    print(ans)
0