結果

問題 No.2280 FizzBuzz Difference
ユーザー vwxyzvwxyz
提出日時 2023-09-12 05:47:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 1,072 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 11,056 KB
実行使用メモリ 8,628 KB
最終ジャッジ日時 2023-09-12 05:47:57
合計ジャッジ時間 1,803 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,404 KB
testcase_01 AC 111 ms
8,576 KB
testcase_02 AC 142 ms
8,628 KB
testcase_03 AC 55 ms
8,028 KB
testcase_04 AC 158 ms
8,576 KB
testcase_05 AC 129 ms
8,092 KB
testcase_06 AC 123 ms
8,060 KB
testcase_07 AC 129 ms
8,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import math

def Extended_Euclid(n,m):
    stack=[]
    while m:
        stack.append((n,m))
        n,m=m,n%m
    if n>=0:
        x,y=1,0
    else:
        x,y=-1,0
    for i in range(len(stack)-1,-1,-1):
        n,m=stack[i]
        x,y=y,x-(n//m)*y
    return x,y

T=int(readline())
for t in range(T):
    M,A,B,K=map(int,readline().split())
    if A<K:
        ans=0
    elif A==K:
        g=math.gcd(A,B)
        ans=max(M//A-1,0)-(M//B-M//(A*B//g))
        if B%A:
            a=M-M%A
            b=M-M%B
            if b%A==0:
                b-=B
            if a<b:
                ans+=1
    else:
        X,Y=Extended_Euclid(A,B)
        g=math.gcd(A,B)
        a=A//g
        b=B//g
        ans=0
        for x,y in ((X,-Y),(-X,Y)):
            if K%g==0:
                x*=K//g
                y*=K//g
                l=max((1-x*A+b*A-1)//(b*A),(1-y*B+a*B-1)//(a*B))
                r=min((M-x*A)//(b*A),(M-y*B)//(a*B))+1
                ans+=max(r-l,0)
        #1<=(x+b*i)*A<=M
        #1<=(y+a*i)*B<=M
    print(ans)
0