HDU 6624 fraction 辗转相除法

fraction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 467    Accepted Submission(s): 249


 

Problem Description

Many problems require printing the probability of something. Moreover, it is common that if the answer is ab , you should output a×b−1(modp) (p is a prime number). In these problems, you cannot know the exact value of the probability. It's so confusing!!! Now, we want to reverse engineer the exact probability from such calculated output value x . We do so by guessing the probability is the one with the minimum b such that a×b−1=x(modp) . Now we invite you to solve this problem with us!

You are given two positive integers p and x , where p is a prime number.

Please find the smallest positive integer b such that there exist some positive integer a satisfying a

 

 

Input

The first line contains an integer T indicating there are T tests. Each test consists of a single line containing two integers: p,x .

* 1≤T≤2×105

* 3≤p≤1015

* p is a prime

* 1

 

 

Output

For each test, output a line containing a string represents the fraction ab using the format "a/b" (without quotes).

 

 

Sample Input

 

3 11 7 998244353 554580197 998244353 998244352

 

 

Sample Output

 

2/5 8/9 499122176/499122177

 

\frac{a}{b}\equiv x(mod \ p)

 

a\equiv bx(mod\ p)

 

a+cp=bx

 

a=bx-cp

 

\because 0<a<b

 

\therefore 0<bx-cp<b

 

\frac{p}{x}<\frac{b}{c}<\frac{p}{x-1}

 

现在要求两个分数间分母最小的分数

 

如果

\frac{p}{x}\ \ \ \ \ \ \ \ \ \ \frac{p}{x-1}之间存在一个整数f 使得  \frac{p}{x}<f<\frac{p}{x-1} 

 

b=f \ \ \ \ c=1

 

 

如果没有的话  两个分数\frac{p}{x}\ \ \ \ \ \ \ \ \ \ \frac{p}{x-1} 都减去f 变成真分数  ,再求倒数 变成假分数

 

类似exgcd 

 

继续寻找答案  最后 再恢复即可

 

参考

HDU 6624 fraction 辗转相除法_第1张图片

 

 

HDU 6624 fraction 辗转相除法_第2张图片

 函数gao(a,b,c,d,x,y)是在求解

 

\frac{a}{b}<\frac{x}{y}<\frac{c}{d}  

 最小的  x  y  

本题是求

 

\frac{p}{x}<\frac{b}{c}<\frac{p}{x-1}

 

你可能感兴趣的:(数论)