HDU1576 A/B 扩展欧几里德简单题

来源:互联网 发布:北风网大数据百度云 编辑:程序博客网 时间:2024/06/06 01:02

本题很简单

A%9973=n;

那么

n=A-A/9973*9973;

设 A/B=X;

代入方程

n=B*X-A/9973*9973

然后这个方程中的 A/9973不要去纠结它,A的值就当不知道,然后 方程可变成二元方程 B*X-9973*Y=n;

这下符合 exgcd的基本式子了 

求出 X, 由于 A/B=X,所以X=A/B,那么  X%9973  其实就是 (A*B)%9973


#include<iostream>#include<cstdio>#include<list>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<stack>#include<map>#include<vector>#include<cmath>#include<memory.h>#include<set>#define ll long long#define LL __int64#define eps 1e-8//const ll INF=9999999999999;#define M 400000100#define inf 0xfffffffusing namespace std;//vector<pair<int,int> > G;//typedef pair<int,int> P;//vector<pair<int,int>> ::iterator iter;////map<ll,int>mp;//map<ll,int>::iterator p;//vector<int>G[30012];LL extgcd(LL a,LL &x,LL b,LL &y){if(b==0){x=1;y=0;return a;}LL r=extgcd(b,x,a%b,y);LL t=x;x=y;y=t-a/b*y;return r;}int main(void){int t;cin>>t;LL n,b;while(t--){cin>>n>>b;LL t0,p0;LL gcd=extgcd(b,t0,9973,p0);/*t0=(t0+n)%n;*/LL t=t0*n/gcd;t=(t%9973+9973)%9973;cout<<t<<endl;}}