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. nginx unit 的使用

    参考文档:http://unit.nginx.org/configuration/# 安装 可以参考这两篇博客: https://www.cnblogs.com/wang-li/p/9694391.h ...

  2. CPP之内存分配

    new & delete expression 1. Introduction A new expression allocates and constructs an object of a ...

  3. 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-fi ...

  4. 第30节:Java基础-内部类

    内部类 // 外部类 class Demo{ private int num = 3; // 定义内部类 class Int{ void show(){ System.out.println(&quo ...

  5. Swift5 语言指南(十四) 下标

    类,结构和枚举可以定义下标,下标是用于访问集合,列表或序列的成员元素的快捷方式.您可以使用下标按索引设置和检索值,而无需单独的设置和检索方法.例如,您可以访问在元素Array实例作为someArray ...

  6. Linux - 结合正则表达式使用grep命令

    Grep with Regular Expression grep命令基本用法 grep [-acinv] [--color=auto] [-A n] [-B n] '搜寻字符串' 文件名参数说明: ...

  7. [视频]K8飞刀 Google黑客功能教程

    [视频]K8飞刀 Google黑客功能教程 链接:https://pan.baidu.com/s/1kbK5jNH8ZaddUEeQ9IwTaw 提取码:lwl6

  8. 【sping揭秘】23、Spring框架内的JNDI支持

    JndiTemplate 经过jdbctemplate,transactionTemplate...的洗礼,想必大家看到template就知道是个什么尿性了吧 一样的,我们只需要调用jnditempl ...

  9. 金三银四季来了!Java 面试题大放送,能答对70%就去BATJTMD试试~

    摘要: 2019,相对往年我们会发现今年猎头电话少了,大部分企业年终奖缩水,加薪幅度也不如往年,选择好offer就要趁早,现在开始准备吧,刷一波Java面试题,能回答70%就去BATJTMD大胆试试~ ...

  10. 判断URL中的中文参数是GB2312还是Utf-8编码

    如两个URL字符串: &q=%E8%A3%99%E5%AD%90&style=grid&seller_type=taobao &q=%CE%D0%C2%D6%D4%F6 ...