Winter D3 D Modular Inverse

Source: Internet
Author: User

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
#include <cstdio>int gcd (int a,int b) {    return b==0?a:gcd (b,a%b);} int main () {    int i,j,k;    int a,m;   int T;   scanf ("%d", &t);   while (t--)   {       scanf ("%d%d", &a,&m);      if (gcd (a,m)!=1) {printf ("not exist\n"); continue;}      In qualifying, it's been here. WA dropped, remember:      //a three B (mod m) is the same as  : a mod m = b mod m      //Can be expressed as A=b+m*k  where K is starting from 0 for      (K=0;K&L t;=1000;k++)      {          if ((m*k+1)%a==0) {printf ("%d\n", (m*k+1)/a);   return 0;}

Winter D3 D Modular Inverse

Contact Us

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.