Suneung Fractions
Time limit1sMemory limit1024 MB
Given x in [A,B] and y in [C,D] up to 1e12, count pairs where the reduced numerator and denominator sum to at most 999.
- Level
Hard10 of 10
- Topics
- Math, Number theory, Binary search, Brute force
- Solved
- No attempts yet
Problem
About 44 days are left before the College Scholastic Ability Test is held. This exam measures students' achievement of National Curriculum standards and the scholastic ability required for college education. (http://www.kice.re.kr/sub/info.do?m=0205&s=english)
One of the subjects covered by this test is Mathematics, which consists of 21 multiple choice questions and 9 short-answer questions. As you can see from the answer sheet below, the answer to each short-answer question is guaranteed to be a unique positive integer below 1000.

However, the organizers might want to give students short-answer questions with non-integer answers, such as or . Usually, the workaround is to write the answer in a canonical form, and then sum up all the integers inside that form and ask students to write that number instead.
In particular, when the answer is a positive rational number , the organizers usually ask students to reduce it and sum up the numerator and the denominator of the reduced fraction. For example, when the answer is , the student should reduce it to and write the final answer as .

However, when the answer is , the reduced fraction is also , so the student should write the final answer as . But this shouldn't happen, since all the answers for the short-answer questions are below 1000. To avoid this situation, the organizers should make sure that after reducing the fraction, the sum of the numerator and the denominator shouldn't exceed . Let's call such fractions Suneung Fractions. For example, and are Suneung fractions, while and are not.
Suppose that, this year, one of the organizers wrote a problem, and the answer to that problem is . Since the problem is not finalized yet, the only thing we know is and holds, for given . The organizers want to know, among all the pairs , how many of is a Suneung fraction. Write a program that counts this number.
Input
The first and only line contains four space-separated integers and (, )
Output
Print the number of integral pairs (, ), where is a Suneung fraction.