結果

問題 No.2280 FizzBuzz Difference
ユーザー titiatitia
提出日時 2023-05-01 02:09:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,616 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-30 08:54:04
合計ジャッジ時間 2,026 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
input = sys.stdin.readline

from math import gcd

# 拡張ユークリッドの互除法.ax+by=gcd(a,b)となる(x,y)を一つ求め、(x,y)とgcd(x,y)を返す.
def Ext_Euc(a,b,axy=(1,0),bxy=(0,1)): # axy=a*1+b*0,bxy=a*0+b*1なので,a,bに対応する係数の初期値は(1,0),(0,1)
    q,r=divmod(a,b)

    if r==0:
        return bxy,b # a*bxy[0]+b*bxy[1]=b
   
    rxy=(axy[0]-bxy[0]*q,axy[1]-bxy[1]*q) # rに対応する係数を求める.
    return Ext_Euc(b,r,bxy,rxy)

# 中国剰余定理(拡張ユークリッドの互除法を使う)
def Chirem(a,ma,b,mb): # N=a mod ma,N=b mod mbのときN=k mod(lcm(ma,mb))なるk,lcm(ma,mb)を返す.
    (p,q),d=Ext_Euc(ma,mb)
    if (a-b)%d!=0:
        return -1 # 解がないとき-1を出力
    return (b*ma*p+a*mb*q)//d%(ma*mb//d),ma*mb//d


T=int(input())

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

    # A, B が互いに素な場合に帰着

    GCD=gcd(A,B)

    if K%GCD!=0:
        print(0)
        continue
    M//=GCD
    A//=GCD
    B//=GCD
    K//=GCD

    ac=M//A
    bc=M//B
    cc=M//(A*B)


    if K>A:
        print(0)
        continue

    if K==A:
        la=M//A*A
        lb=M//B*B

        if lb>la:
            print(ac+bc-cc-1-((bc-cc)*2)-1)
        else:
            print(ac+bc-cc-1-((bc-cc)*2))
        continue

    if K<A:

        ANS=0

        q=Chirem(K%A,A,0,B)[0]
        ANS+=M//(A*B)

        if M//(A*B)*(A*B)+q<=M:
            ANS+=1
        
        q=Chirem(0,A,K%B,B)[0]
        ANS+=M//(A*B)

        if M//(A*B)*(A*B)+q<=M:
            ANS+=1

        print(ANS)
0