转化为e_gcd(1576)

来源:互联网 发布:怎么申请淘宝网店 编辑:程序博客网 时间:2024/05/14 23:35

A/B

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3707    Accepted Submission(s): 2835


Problem Description
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
 

Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
 

Output
对应每组数据输出(A/B)%9973。
 

Sample Input
21000 5387 123456789
 

Sample Output
79226060

原题要求(A/B)%9973且n=A%9973 -->Bx=A --> Bx=n (mod 9973) --> Bx+9973y=n 求解x在(0,9973)的最小整数解,所以这就化成了一个标准的扩展欧几里德算法的乘法逆元。原题还有条件:gcd(B,9973)=1。所以此不定方程必有解。


/*------------------Header Files------------------*/#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <cstdlib>#include <ctype.h>#include <cmath>#include <stack>#include <queue>#include <deque>#include <map>#include <vector>#include <set>#include <limits.h>using namespace std;/*------------------Definitions-------------------*/#define LL long long#define uLL unsigned long long#define PI acos(-1.0)#define INF 0x3F3F3F3F#define MOD 9973#define MAX 100050#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r/*---------------------Work-----------------------*/LL e_gcd(LL a,LL b,LL &x,LL &y){if(b==0){x=1,y=0;return a;}LL ans=e_gcd(b,a%b,x,y);LL temp=x;x=y;y=temp-a/b*y;return ans;}LL cal(LL a,LL b,LL c){LL x,y;LL gcd=e_gcd(a,b,x,y);if(c%gcd!=0) return -1;x*=c/gcd;b/=gcd;if(b<0) b=-b;LL ans=x%b;if(ans<=0) ans+=b;return ans;}void work(){int T; cin>>T;while(T--){int n;LL b;scanf("%d%I64d",&n,&b);printf("%I64d\n",cal(b,9973,n));}}/*------------------Main Function------------------*/int main(){//freopen("test.txt","r",stdin);work();return 0;}



0 0
原创粉丝点击