結果

問題 No.1899 L1 Cafe
ユーザー 👑 KazunKazun
提出日時 2022-04-08 22:45:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 481 ms
コンパイル使用メモリ 87,296 KB
実行使用メモリ 83,464 KB
最終ジャッジ日時 2023-08-19 01:13:55
合計ジャッジ時間 5,091 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,276 KB
testcase_01 AC 227 ms
82,024 KB
testcase_02 AC 202 ms
83,268 KB
testcase_03 AC 182 ms
80,296 KB
testcase_04 AC 191 ms
80,008 KB
testcase_05 AC 167 ms
79,348 KB
testcase_06 AC 218 ms
82,920 KB
testcase_07 AC 209 ms
80,700 KB
testcase_08 AC 210 ms
80,484 KB
testcase_09 AC 171 ms
79,472 KB
testcase_10 AC 224 ms
81,592 KB
testcase_11 AC 173 ms
83,464 KB
testcase_12 AC 220 ms
81,456 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