
本文共 1749 字,大约阅读时间需要 5 分钟。
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
311 7998244353 554580197998244353 998244352
Sample Output
2/58/9499122176/499122177
题意: 给出一个p和x(代码里用a代替)要求求出最小的b满足 输出a/b的形式
题解:
推到这里就是一个经典水题了~~主要是推导过程+你知道这个算法 剩下的就是带入到算法里面就好了~
上代码:
#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;}