# ZOJ 3609 Modular Inverse

Source: Internet
Author: User

Surface:

Modular Inverse Time limit: 2 Seconds Memory Limit: 65536 KB

The modular modular multiplicative inverse of an integer a modulo are an m integer x such that `a-1≡x (mod m)` . This was equivalent to `ax≡1 (mod m)` .

Input

There is multiple test cases. The first line of input was an integer T ≈2000 indicating the number of test cases.

Each test case contains integers 0 < a ≤1000 and 0 < m ≤1000.

Output

For each test case, output the smallest positive x . If such x doesn ' t exist, output "not exist".

Sample Input
`33 114 125 13`
Sample Output
`4Not Exist8`

Solving:

At first glance, it should be the Chinese remainder theorem, or what linear congruence equation, but the original learning this piece did not learn, carefully see can water, and so on after review, then to supplement other solutions.

Ideas:

Because (a*x)%m==1%m, can write (A * (x ' +n*m))%m==1%m, when the value of X is more than M, actually has been repeated, and M in the range of 0 to 1000, so the direct burst is good.

Compared to the pit is, when writing, directly on the right to write 1, did not take into account the situation of m=1. Therefore, the details are quite important, before submitting, you need to carefully examine the boundary data.

Code:

`#include <iostream> #include <algorithm> #include <string> #include <iomanip> #include < Cstdio> #include <cstring>using namespace Std;int main () {    int t,a,m,x;    BOOL Flag;    scanf ("%d", &t);    while (t--)    {    scanf ("%d%d", &a,&m);    if (m==1) {  printf ("1\n");  Continue;}        Flag=false;        for (int i=1;i<=m;i++)        {           if ((a*i)%m==1)           {             flag=true;             printf ("%d\n", I);             break;           }        }        if (!flag) printf ("Not exist\n");    } return 0;}`

ZOJ 3609 Modular Inverse

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.