Farey Sequence Sum
Time limit1sMemory limit256 MB
Given N, compute the sum of consecutive denominator ratios in the N-th Farey sequence and print it as a reduced fraction.
- Level
Hard8 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Problem
For a positive integer , take every irreducible fraction with and , together with and , and sort them in increasing order. The result is the -th Farey sequence.
For example, the 6th Farey sequence is:
Writing down only the denominators of the -th Farey sequence, in order, gives a sequence of length . The Farey sum is the total of over through .
The sum for the 6th Farey sequence works out like this:
Given , compute the sum of the -th Farey sequence.
Input
The first line holds the number of test cases . ()
Each of the next lines holds a test case number and the value described above, separated by one space. (, )
Output
For each test case, print the test case number given in the input and the Farey sum on one line, separated by one space. Reprint the number exactly as it appears in the input, and keep the lines in input order.
Always write the sum as a reduced fraction in the form numerator/denominator. If the reduced denominator is 1, print only the numerator.
Hint
The -th Farey sequence contains every fraction of the -th one, plus as many new fractions as there are positive integers up to that are coprime with .