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<b and a≡bx(modp) .

 

 

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<x<p

 

 

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 

 

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

 

参考

 

 

 函数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}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值