Modular Inverse


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

Input

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

Each test case contains two 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

3
3 11
4 12
5 13

Sample Output

4
Not Exist
8

References


Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

首先我来回顾下欧几里德的几个定理,有助于理解这道题;

定理一:如果d = gcd(a, b),则必能找到正的或负的整数k和l,使 d = a*x+ b*y。

定理二:若gcd(a, b) = 1,则方程ax ≡ c (mod b)在[0, b-1]上有唯一解。

定理三:若gcd(a, b) = d,则方程ax ≡ c (mod b)在[0, b/d - 1]上有唯一解。

转载请注明出处:寻找&星空の孩子

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712

对于ax+by=1;  即ax=1(mod b)      当且仅当gcd(a,b)!=1 的时候,无解!

 #include<stdio.h>

 void exgcd(int a,int b,int &d,int &x,int &y)
{
if(!b){d=a;x=;y=;}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
int main()
{
int T,a,m;
scanf("%d",&T);
while(T--)
{
int d,x,y;
scanf("%d%d",&a,&m);
exgcd(a,m,d,x,y);
if(d==)
{
while(x<=)
{
x+=m/d;
}
printf("%d\n",x);
}
else
printf("Not Exist\n");
}
return ;
}

Modular Inverse(zoj3609+欧几里德)的更多相关文章

  1. Modular Inverse(模逆元,扩展欧几里德)

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  2. ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  3. 寒假 D3 D Modular Inverse

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

  4. zjuoj 3609 Modular Inverse

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...

  5. ZOJ——3609 Modular Inverse

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  6. ZOJ 3609 Modular Inverse(扩展欧几里德)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...

  7. [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]

    Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...

  8. 【ZOJ 3609】Modular Inverse 最小乘法逆元

    The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x  ...

  9. B - Modular Inverse

    The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x ...

随机推荐

  1. 内置函数_map()、reduce()、filter()

    map().reduce().filter() map()内置函数把一个函数func依次映射到序列或迭代器对象的每个元素上,并返回一个可迭代的map对象作为结果,map对象中每个元素是原序列中元素经过 ...

  2. css3的动画效果

    全新的css3加入的动画效果: [ animation-name ]:检索或设置对象所应用的动画名称 [ animation-duration ]: 检索或设置对象动画的持续时间 [ animatio ...

  3. 如何将已有的本地Git 库推送到远端仓库?

    以Github 为例 step 1. 在Github建立一个空的仓库 Step 2. 建立远端仓库的别名 >$ git remote add origin https://github.com/ ...

  4. .net core mvc发布项目到IIS上出现500错误

    如题,我把.net core mvc项目以应用程序方式挂到IIS默认网站下,结果出现了如下错误:HTTP Error 500.0 - ANCM In-Process Handler Load Fail ...

  5. Shell-6--预定义变量

  6. C++Primer笔记之复制控制

    复制控制这一节需要注意的地方不多,主要有以下几点: 1.定义自己的复制构造函数 什么时候需要定义自己的复制构造函数,而不用系统提供的,主要遵循以下的经验说明: 某些类必须对复制对象时发生的事情加以控制 ...

  7. Windows Azure开发之Linux虚拟机

     Windows Azure是微软的云服务集合,用来提供云在线服务所需要的操作系统与基础存储与管理的平台,是微软的云计算的核心组成组件之一.其中windows azure提供的最重要的一项服务就是 ...

  8. LeetCode--No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  9. Hive SQL基础操作

    创建表 hive 查看本地的文件#Can execute local commands within CLI, place a command in between ! and ;!cat data/ ...

  10. PHP-----浅谈垃圾回收机制

    前言 大多数编程语言都会有自身的垃圾回收机制,php也不例外.经常听很多人说gc,也就是垃圾回收器,全程为Garbage Collection. 在php5.3之前,是不包括垃圾回收机制的,也没有专门 ...