The Lost Fraction
Time limit1sMemory limit128 MB
Given n and a, list every reduced fraction with denominator at most n that lies in the closed interval [a/n^2, (a+1)/n^2], in increasing order.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Binary search
- Solved
- No attempts yet
Problem
Byteasar came up with a really neat fraction whose denominator is at most , but after a while he forgot which fraction it was. He is almost certain that it lay inside the closed interval . Help him by finding every fraction inside this interval whose denominator is at most .
Here a fraction "with denominator at most " means a rational number whose denominator, once the fraction is reduced to lowest terms, is at most . Both endpoints of the interval are included.
Input
The only line contains two integers and separated by a single space (, ).
Output
On the first line print , the number of distinct reduced fractions that lie in the interval and have denominator at most .
On each of the following lines print the numerator and the denominator of one such fraction, separated by a single space. Every fraction must be written in lowest terms, and the fractions must be listed in strictly increasing order of their value.