#abc333g. G - Nearest Fraction

G - Nearest Fraction

Score : 625625 points

问题陈述

给定一个小于1的正实数 rr 以及一个正整数 NN

在满足条件 (p,q)(p,q) 的整数对中,其中 0pqN0\leq p\leq q\leq N 并且 gcd(p,q)=1\gcd(p,q)=1(即 ppqq 互质),找出使得 rpq\left\vert r-\dfrac pq\right\vert 最小的那一对。

如果存在多个满足条件的整数对 (p,q)(p,q),则输出其中 pq\dfrac pq 值最小的那个对。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a positive real number rr less than 11, and a positive integer NN.

Among the pair of integers (p,q)(p,q) such that 0pqN0\leq p\leq q\leq N and gcd(p,q)=1\gcd(p,q)=1, find the one that minimizes rpq\left\vert r-\dfrac pq\right\vert.

If multiple such pairs (p,q)(p,q) exist, print the one with the smallest value of pq\dfrac pq.

Constraints

  • 0<r<10\lt r\lt 1
  • rr is given as a real number with at most 1818 decimal places.
  • 1N10101\leq N\leq 10^{10}
  • NN is an integer.

Input

The input is given from Standard Input in the following format:

rr

NN

Output

For the pair (p,q)(p,q) that satisfies the conditions in the problem statement, print pp and qq in this order, separated by a space, in a single line.

Sample Input 1

0.333
33

Sample Output 1

1 3

0.33313=13000\left\vert0.333-\dfrac13\right\vert=\dfrac1{3000}. There is no 0pq33,gcd(p,q)=10\leq p\leq q\leq33,\gcd(p,q)=1 such that $\left\vert0.333-\dfrac pq\right\vert\lt\dfrac1{3000}$, so print 1 3.

Sample Input 2

0.45
5

Sample Output 2

2 5

$\left\vert0.45-\dfrac12\right\vert=\left\vert0.45-\dfrac25\right\vert=\dfrac1{20}$. There is no 0pq5,gcd(p,q)=10\leq p\leq q\leq5,\gcd(p,q)=1 such that 0.45pq<120\left\vert0.45-\dfrac pq\right\vert\lt\dfrac1{20}, and we have 12>25\dfrac12\gt\dfrac25, so print 2 5.

Sample Input 3

0.314159265358979323
10000

Sample Output 3

71 226

$\left\vert0.314159265358979323-\dfrac{71}{226}\right\vert=\dfrac{3014435336501}{113000000000000000000}$.

Sample Input 4

0.007735339533561113
7203576162

Sample Output 4

34928144 4515398949

update @ 2024/3/10 01:20:36