結果

問題 No.1899 L1 Cafe
ユーザー 👑 KazunKazun
提出日時 2022-04-08 22:45:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 82,544 KB
最終ジャッジ日時 2024-05-06 08:23:00
合計ジャッジ時間 3,717 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,096 KB
testcase_01 AC 206 ms
80,072 KB
testcase_02 AC 178 ms
81,488 KB
testcase_03 AC 157 ms
78,380 KB
testcase_04 AC 167 ms
78,348 KB
testcase_05 AC 138 ms
77,804 KB
testcase_06 AC 193 ms
81,280 KB
testcase_07 AC 192 ms
78,396 KB
testcase_08 AC 190 ms
78,880 KB
testcase_09 AC 147 ms
78,160 KB
testcase_10 AC 193 ms
79,768 KB
testcase_11 AC 151 ms
82,544 KB
testcase_12 AC 197 ms
80,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Linear_Sum(a,b,L,R):
    """ Sum_{i=L}^R (ai+b) を求める.
    """

    if L<=R:
        return (a*(L+R)+2*b)*(R-L+1)//2
    else:
        return 0

def floor_sum(A,B,M,N):
    """sum_{i=0}^{N-1} floor((A*i+B)/M) を求める.
    """
    T=0
    while True:
        T+=((N-1)*N//2)*(A//M)
        A%=M

        T+=N*(B//M)
        B%=M

        y=(A*N+B)//M
        x=B-y*M

        if y==0:
            return T

        T+=(N+x//A)*y
        A,B,M,N=M,x%A,A,y

def Floor_Sum(A:int,B:int,M:int,N:int,K=0):
    """sum_{i=K}^N floor((A*i+B)/M) を求める.
    """

    if K==0:
        return floor_sum(A,B,M,N+1)
    else:
        return floor_sum(A,B,M,N+1)-floor_sum(A,B,M,K)
#==================================================
import sys
input=sys.stdin.readline
write=sys.stdout.write

T=int(input())
Ans=[0]*T
for t in range(T):
    N,A,B=map(int,input().split())

    # X 軸に平行な道路上
    Ans[t]+=Linear_Sum(-B,N,1,N//B)

    # Y 軸に平行な道路上
    Ans[t]+=Linear_Sum(-A,N,1,N//A)

    # 道路の交差点
    Ans[t]-=Floor_Sum(-A,N,B,N//A,1)

write("\n".join(map(str,Ans)))
0