杭电2019多校第五场 HDU——6624 fraction(辗转相除法)

Many problems require printing the probability of something. Moreover, it is common that if the answer is abab, you should output a×b−1(modp)a×b−1(modp) (pp 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 xx. We do so by guessing the probability is the one with the minimum bb such that a×b−1=x(modp)a×b−1=x(modp). Now we invite you to solve this problem with us! 

You are given two positive integers pp and xx, where pp is a prime number. 

Please find the smallest positive integer bb such that there exist some positive integer aa satisfying a<ba<b and a≡bx(modp)a≡bx(modp).

Input

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

* 1≤T≤2×1051≤T≤2×105 

* 3≤p≤10153≤p≤1015 

* pp is a prime 

* 1<x<p1<x<p

Output

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

Sample Input

3
11 7
998244353 554580197
998244353 998244352

Sample Output

2/5
8/9
499122176/499122177

题意: 给出一个p和x(代码里用a代替)要求求出最小的b满足 a \equiv bx(mod \;p)  输出a/b的形式

题解:

a \equiv bx(mod \;p) \par a+pc=bx \par a=bx-pc (c \in \;z_+) \par \because 0<a<b \par \rightarrow 0 < bx-pc < b \par \rightarrow bx-b<pc \quad pc<bx

\rightarrow b<\frac{pc}{x-1} \quad \frac{pc}{x}<b

\rightarrow \frac{pc}{x}<b<\frac{pc}{x-1};

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

推到这里就是一个经典水题了~~主要是推导过程+你知道这个算法 剩下的就是带入到算法里面就好了~

上代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
void zzxc(ll pa,ll pb,ll qa,ll qb,ll &x,ll &y){//辗转相除法求: "pa/pb < x/y <= qa/qb"  的min(y),min(x)
	ll z=(pa+pb-1)/pb;
	if(z<=qa/qb){
		x=z;y=1;
		return;
	}
	pa-=(z-1)*pb;qa-=(z-1)*qb;
	zzxc(qb,qa,pb,pa,y,x);
	x+=(z-1)*y;
	return;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll p,a,x,y;
		scanf("%lld%lld",&p,&a);
		zzxc(p,a,p,a-1,x,y);//对应带入
		printf("%lld/%lld\n",x*a-p*y,x);//这里(带入公式)对应输出,不需要约分,注意:别把符号看乱了
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值