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

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

代码例如以下:

#include <cstdio>
#include <cstring>
#include <cmath>
typedef long long LL; LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
LL r = exgcd(b,a%b,x,y);
LL t = x;
x = y;
y = t-a/b*y;
return r;
}
} LL cal(LL a, LL b, LL c)
{
LL x, y;
LL tt = exgcd(a, b, x, y);
if(c%tt)//无整数解
{
return -1;
}
x*=c/tt;
b/=tt;
if(b<0)
b=-b;
LL ans=x%b;
if(ans<=0)
ans+=b;
return ans;
} int main()
{
LL a, b, t;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&a,&b);
LL ans = cal(a, b, 1);
if(ans == -1)
{
printf("Not Exist\n");
continue;
}
printf("%lld\n",ans);
}
return 0;
}

ZOJ 3609 Modular Inverse(扩展欧几里德)的更多相关文章

  1. ZOJ 3609 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. ZOJ 3609 Modular Inverse(扩展欧几里得)题解

    题意:求乘法逆元最小正正数解 思路:a*x≡1(mod m),则称x 是 a 关于 m 的乘法逆元,可以通过解a*x + m*y = 1解得x.那么通过EXGcd得到特解x1,最小正解x1 = x1 ...

  4. ZOJ 3609 Modular Inverse

    点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...

  5. zjuoj 3609 Modular Inverse

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

  6. 【ZOJ】3609 Modular Inverse

    1. 题目描述求乘法逆元. 2. 基本思路利用扩展gcd求逆元,模板题目. 3. 代码 /* 3609 */ #include <iostream> #include <sstrea ...

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

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

  8. Modular Inverse(zoj3609+欧几里德)

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

  9. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

随机推荐

  1. ffmpeg处理网络流

    最近遇到好几个人在问ffmpeg如何处理网络流,刚好前段时间也在做这方面,抽空整理了下,把主要代码发出来,希望对大家有用.为简单处理,我这里只简单介绍UDP接收TS流,其实只要是socket接收的都可 ...

  2. vsftp虚拟用户方式访问

    需求:外部人员需要对公司服务器上某个文件夹内容进行读写操作 文件目录信息:/opt/abc drwxr-xr-x 9 www  www       4096 12月  4 13:02 abc   #注 ...

  3. 微信小程序 设置计时器(setInterval)、清除计时器(clearInterval)

    1.wxml代码 <!--index.wxml--> <view class="container"> <button type='primary' ...

  4. find -print0和xargs -0原理及用法

    平常我们经常把find和xargs搭配使用,例如: find . -name "*.txt" | xargs rm 但是这个命令如果遇到文件名里有空格或者换行符,就会出错.因为xa ...

  5. python爬虫学习,使用requests库来实现模拟登录4399小游戏网站。

    1.首先分析请求,打开4399网站. 右键检查元素或者F12打开开发者工具.然后找到network选项, 这里最好勾选perserve log 选项,用来保存请求日志.这时我们来先用我们的账号密码登陆 ...

  6. POJ 1741 Tree【Tree,点分治】

    给一棵边带权树,问两点之间的距离小于等于K的点对有多少个. 模板题:就是不断找树的重心,然后分开了,分治,至少分成两半,就是上限为log 然后一起统计就ok了 #include<iostream ...

  7. wait和waitpid函数

    来源:http://hohahohayo.blog.163.com/blog/static/120816010200971210230362/ wait(等待子进程中断或结束)表头文件     #in ...

  8. ***mysql 用一个表的一列,去更新另一表的一列

    需求: 老板给了一个EXCEL数据,是本人提供的一个模板,含ID,现在相当于要导入这新增的一列数据到数据库中的某一个表. 方法一:用navicat,在excel中复制一列,再粘贴到navicat中的一 ...

  9. POJ——T 2976 Dropping tests

    http://poj.org/problem?id=2976 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13861   ...

  10. 学习日常笔记<day09>Http协议

    1 Http协议入门 1.1 什么是http协议 http协议: 对浏览器客户端 和  服务器端 之间数据传输的格式规范 1.2 查看http协议的工具 1)使用火狐的firebug插件(右键-> ...