Sicily 1903. Farey Sequence Again

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/83

1/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;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值