1903. Farey Sequence Again
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
The Farey sequence Fn for any positive integer n is the set of irreducible rational numbers a/b with 0<a<b<=n and (a, b) = 1 arranged in increasing order. Here (a, b) mean the greatest common divisor of a and b. For example:
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
Given two positive integers N and K, you need to find out the K-th smallest element of the Farey sequence FN.
Input
The first line of input is the number of test case T, 1<=T<=1000. Then each test case contains two positive integers N and K. 1<=K<N<=10^9.
Output
For each test case output the Kth smallest element of the Farey sequence FN in a single line.
Sample Input
3 2 1 100 68 101 69
Sample Output
1/2 2/831/42
http://blog.163.com/moon_goddess_2003/blog/static/225157172201311811235261/
// Problem#: 1903 // Submission#: 3580805 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <stdio.h> int main() { int t, n, k; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); int num1 = n / 2 + 1; if (k <= num1) printf("1/%d\n", n + 1 - k); else { k -= num1; int num2 = n & 1 ? n - 2 * n / 3 : n - 1 - 2 * n / 3; if (k <= num2) { int tmp = n & 1 ? n + 1 - k : n - k; if (tmp & 1) printf("2/%d\n", tmp); else printf("1/%d\n", tmp / 2); } else { k -= num2; if (n % 3 == 0) { if (k == 1) { printf("1/%d\n", n / 3); continue; } else { k--; n--; } } if (n % 3 == 1) { n *= 2; n -= (k - 1) / 4 * 6; if (k % 4 == 1) printf("3/%d\n", n / 2); else if (k % 4 == 2) printf("1/%d\n", (n - 2) / 6); else if (k % 4 == 3) printf("3/%d\n", (n - 4) / 2); else printf("2/%d\n", (n - 5) / 3); } else { n *= 2; n -= (k - 1) / 4 * 6; if (k % 4 == 1) printf("3/%d\n", n / 2); else if (k % 4 == 2) printf("2/%d\n", (n - 1) / 3); else if (k % 4 == 3) printf("3/%d\n", (n - 2) / 2); else printf("1/%d\n", (n - 4) / 6); } } } } return 0; }