ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 17417 |
문제명 | Optimization is Freaky Fun |
레벨 | 플래티넘 1 |
분류 |
정수론 |
시간복잡도 | O(Q*sqrt(N)) [본문 참고] |
인풋사이즈 | Q<=10, N<=10^12 [본문 참고] |
사용한 언어 | PyPy |
제출기록 | 230452KB / 1440ms |
최고기록 | 740ms |
해결날짜 | 2021/06/09 |
"""Solution code for "BOJ 17417. Optimization is Freaky Fun".
- Problem link: https://www.acmicpc.net/problem/17417
- Solution link: http://www.teferi.net/ps/problems/boj/17417
This code should be submitted with PyPy3. Python3 gets TLE.
"""
import bisect
import sys
N_THRESHOLD = 10**5
Q_THRESHOLD = 10
INF = float('INF')
def calc_with_prefix_sum(n, s, e, prefix_sum):
if not prefix_sum:
psum = 0
i = 1
while i <= n:
j = n // (n // i)
psum += n // i * (j - i + 1)
prefix_sum.append((j, psum))
i = j + 1
i, psum = prefix_sum[bisect.bisect(prefix_sum, (e, INF)) - 1]
sum_to_e = psum + (e - i) * (n // e)
if s == 1:
return sum_to_e
else:
s -= 1
i, psum = prefix_sum[bisect.bisect(prefix_sum, (s, INF)) - 1]
sum_to_s = psum + (s - i) * (n // s)
return sum_to_e - sum_to_s
def main():
Q = int(sys.stdin.readline())
prefix_sum = []
for _ in range(Q):
N, S, E = [int(x) for x in sys.stdin.readline().split()]
if E < S or N < S:
print(0)
continue
if Q > Q_THRESHOLD and N > N_THRESHOLD:
# For this case, all queries should have same N value.
print(calc_with_prefix_sum(N, S, E, prefix_sum))
continue
i = S
answer = 0
while i <= N:
j = N // (N // i)
if j >= E:
answer += N // i * (E - i + 1)
break
answer += N // i * (j - i + 1)
i = j + 1
print(answer)
if __name__ == '__main__':
main()