# K-A/B (reverse element)

Source: Internet
Author: User

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;}

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.