杭电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;
} 
发布了195 篇原创文章 · 获赞 27 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lgz0921/article/details/98621101