PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000293. Tổng phần nguyên (Floor Sum)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng phần nguyên (Floor Sum)
Floor Sum
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given four integers . For every index with , take the floor of .
Compute the exact integer sum
$$S=\sum_{i=0}^{N-1}\left\lfloor\frac{Ai+B}{M}\right\rfloor.$$Here is the largest integer not greater than . Do not take the answer modulo any number.
Input
One line containing four integers , , , , in that order.
Output
Print one integer , the requested sum.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
4 10 6 3
Output
3
Explanation
For , the numerators are . Dividing by and taking the floors yields . Their sum is .
Input
1000000000 1000000000 999999999 999999999
Output
499999999500000000
Explanation
Since and , the floor of is . Thus the answer is .