結果

問題 No.752 mod数列
ユーザー mkawa2
提出日時 2021-02-05 11:22:14
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 16,256 KB
最終ジャッジ日時 2024-07-02 00:53:11
合計ジャッジ時間 9,771 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def MI1(): return map(int1, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)]]
inf = 10**16
# md = 998244353
md = 10**9+7

from bisect import bisect

p, q = MI()
lim = min(100000, p+1)
pre = [0]
for m in range(1, lim):
    pre.append(pre[-1]+p%m)
# print(pre)

mm = [lim-1]
pos = [pre[-1]]
for f in range(p//lim, 0, -1):
    m = p//f
    n = m-mm[-1]
    s = n*p-f*(mm[-1]+1+m)*n//2
    mm.append(m)
    pos.append(pos[-1]+s)
# print(mm)
# print(pos)

def sigma(r):
    if r < lim: return pre[r]
    i = bisect(mm, r)
    n = r-mm[i-1]
    res = pos[i-1]+n*p-p//r*(mm[i-1]+1+r)*n//2
    return res

for _ in range(q):
    l, r = MI()
    print(sigma(r)-sigma(l-1))
0