#CT0000065. Đếm căn theo lớp dư - EP1 (Counting Square Roots by Residue Class - EP1)
Đếm căn theo lớp dư - EP1 (Counting Square Roots by Residue Class - EP1)
Counting Square Roots by Residue Class - EP1
Version: Phuoc Hung OJ Extended
Problem Statement
You are given four integers with .
Count the positive integers satisfying both
and
Input
One line containing four integers .
Output
Print one integer: the number of positive integers satisfying both conditions.
Subtasks
- Subtask 1 (25 points): .
- Subtask 2 (35 points): the integer-root interval that must be considered has length at most .
- Subtask 3 (40 points): , , .
Examples
Example 1
Input
1 30 2 0
Output
2
Explanation
The positive roots whose squares lie in are . Among them, 2 and 4 are congruent to 0 modulo 2, so the answer is 2.
Example 2
Input
10 100 2 1
Output
3
Explanation
The square condition gives . The odd values in this interval are 5, 7, and 9, so the answer is 3.
Example 3
Input
1 100 3 1
Output
4
Explanation
For , the values congruent to 1 modulo 3 are 1, 4, 7, and 10. Hence the answer is 4.
Example 4
Input
50 80 5 0
Output
0
Explanation
Only has its square in , but , so no value is valid.
Example 5
Input
999998000001 1000000000000 1000000 0
Output
1
Explanation
The boundary roots are 999999 and 1000000. Only 1000000 is congruent to 0 modulo 1000000, so the answer is 1.