UVA10408 Farey sequences【数列】

A fraction h/k is called a proper fraction if it lies between 0 and 1 and if h and k have no common factors. For any natural number n ≥ 1, the Farey sequence of order n, Fn, is the sequence of all proper fractions with denominators which do not exceed n together with the “fraction” 1/1, arranged in increasing order. So, for example, F5 is the sequence:

  It is not clear exactly who first thought of looking at such sequences. The first to have proved genuine mathematical results about them seems to be Haros, in 1802. Farey stated one of Haros’ results without a proof in an article written in 1816, and when Cauchy subsequently saw the article he discoverd a proof of the result and ascribed the concept to Farey, thereby giving rise to the name Farey sequence. Hardy in his A mathematician’s apology writes:

      ... Farey is immortal because he failed to understand a theorem which Haros had proved perfectly fourteen years before ...

  Surprisingly, certain simple looking claim about Farey sequences is equivalent to the Riemann hypothesis, the single most important unsolved problem in all of mathematics. Ford circles, see picture, provide a method of visualizing the Farey sequence.

  But your task is much simpler than this.

  For a given n, you are to find the k-th fraction in the sequence Fn.

Input

Input consists of a sequence of lines containing two natural numbers n and k, 1 ≤ n ≤ 1000 and k suffi- ciently small such that there is the k-th term in Fn. (The length of Fn is approximately 0.3039635n^2 ).

Output

For each line of input print one line giving the k-th element of Fn in the format as below.

Sample Input

5 5

5 1

5 9

5 10

117 348

288 10000

Sample Output

1/2

1/5

4/5

1/1

9/109

78/197

问题链接UVA10408 Farey sequences

问题简述:(略)

问题分析

  有关Farey数列,可以参考维基百科的Farey sequences

  解体程序可以参考其中的Python语言程序改写。

程序说明:(略)

题记:(略)

参考链接:(略)

Farey sequences的Python语言程序如下:

def farey_function(n, descending=False):
    """Print the nth Farey sequence, either ascending or descending."""
    a, b, c, d = 0, 1, 1, n
    if descending: 
        a, c = 1, n-1
    print "%d/%d" % (a,b)
    while (c <= n and not descending) or (a > 0 and descending):
        k = int((n + b) / d)
        a, b, c, d = c, d, (k*c-a), (k*d-b)
        print "%d/%d" % (a,b)

AC的C++语言程序如下:

/* UVA10408 Farey sequences */

#include <iostream>

using namespace std;

void farey(int n, int m)
{
    int a = 0, b = 1, c = 1, d = n, c2, d2;
    for (int i = 1; i < m; i++) {
        int k = (n + b) / d;
        c2 = k * c - a;
        d2 = k * d - b;
        a = c, c = c2;
        b = d, d = d2;
    }
    printf("%d/%d\n", c, d);
}

int main()
{
    int n, k;
    while(~scanf("%d%d", &n, &k))
        farey(n, k);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81391328
今日推荐