Description
(A/B) % 9973, but because a is very large, we only give n (n = A % 9973) (the given a must be divisible by B, and gcd (B, 9973) = 1 ).
Input
The first row of data is a T, indicating that there is a T group of data.
Each data set has two data sets: n (0 <= n <9973) and B (1 <= B <= 10 ^ 9 ).
Output
Corresponding to each group of data output (a/B) % 9973.
Sample Input
21000 5387 123456789
Sample output
79226060
Solution:
This question should be extended by Euclidean algorithm. The general idea is (n = 9973) :( a/B) % N = (a % N) * (1/B) % N) % N
Because gcd (B, n) = 1, so = (N * (gcd (B, n)/B) % N) %
In addition, according to the Extended Euclidean algorithm, a group of X and Y can be obtained, so that gcd (B, n) = B * x + N * y can be substituted into the above formula.
(N * (x + N * Y/B) % N = (n % N) * (x + N * Y/B) % N) % N = (N * (x + N * Y/B) % N
Because Y <B, Y/B = 0, so the original G = (N * X) % N
Because X may be negative, as long as G = (G % N + n) % N, it can be converted to positive.
The Code is as follows:
#include<stdio.h>#include <iostream>using namespace std;int exgcd(int a,int b,int &x,int &y){ int r; int t; if(b==0) { x=1; y=0; return a; } r=exgcd(b,a%b,x,y); t=x; x=y; y=t-a/b*y; return r;}int main(){ int T,n,b,x,y; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&b); exgcd(b,9973,x,y); x*=n; x=(x%9973+9973)%9973; printf("%d\n",x%9973); } return 0;}