結果

問題 No.1899 L1 Cafe
ユーザー 👑 rin204rin204
提出日時 2022-04-08 22:21:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 402 ms / 2,000 ms
コード長 860 bytes
コンパイル時間 909 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 79,888 KB
最終ジャッジ日時 2023-08-19 00:50:55
合計ジャッジ時間 6,453 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,156 KB
testcase_01 AC 402 ms
79,100 KB
testcase_02 AC 369 ms
78,824 KB
testcase_03 AC 277 ms
79,776 KB
testcase_04 AC 307 ms
79,372 KB
testcase_05 AC 244 ms
79,696 KB
testcase_06 AC 402 ms
78,956 KB
testcase_07 AC 379 ms
79,612 KB
testcase_08 AC 375 ms
79,888 KB
testcase_09 AC 314 ms
78,704 KB
testcase_10 AC 368 ms
79,472 KB
testcase_11 AC 376 ms
79,340 KB
testcase_12 AC 385 ms
79,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# return (\sum_{i=0}^{n-1} ((a*i+b)//m))
def floor_sum(n, m, a, b):
    ret = 0
    while True:
        if a >= m:
            ret += ((n - 1) * n) // 2 * (a // m)
            a %= m
        if b >= m:
            ret += n * (b // m)
            b %= m
        y_max = (a * n + b) // m
        if y_max == 0:
            return ret
        x_max = y_max * m - b
        ret += (n - (x_max + a - 1) // a) * y_max
        n, m, a, b = y_max, a, m, -x_max % a

def solve():
    n, a, b = map(int, input().split())
    ans = 0
    max_ = n - a
    min_ = n % a
    if max_ > 0:
        ans += (max_ + min_) * (max_ - min_ + a) // a // 2
        
    max_ = n - b
    min_ = n % b
    if max_ > 0:
        ans += (max_ + min_) * (max_ - min_ + b) // b // 2
    
    ans -= floor_sum(n // a, b, a, n % a)
    print(ans)
    
for _ in range(int(input())):
    solve()
0