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
#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;}
//排位赛的时候一直在这里wa掉,谨记:
//a三b(mod m) 是同余 即:a mod m == b mod m
//可表示成 a=b+m*k 其中k是从 0 开始
for(k=0;k<=1000;k++)
{
if((m*k+1)%a==0) {printf("%d\n",(m*k+1)/a);break;}
}
}
return 0;
}

寒假 D3 D Modular Inverse的更多相关文章

  1. zjuoj 3609 Modular Inverse

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

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

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

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

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

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

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

  5. ZOJ——3609 Modular Inverse

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

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

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

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

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

  8. B - Modular Inverse

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

  9. Modular Inverse (拓展欧几里得求逆元)

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

随机推荐

  1. 关于visio 2007导入独立图库

    很多作网络拓扑或服务器系统拓扑时,我们都会找到相关的Visio图库来画,但很多时候我们不知如何才能够直接导入,下面是我自己的导入方式,供大家参考下! 打开07Visio,自动加载设置: 工具--> ...

  2. VIM中的折叠命令

    参考:http://blog.csdn.net/bruce0532/article/details/8497284 za:在折叠与展开间自由转换 zf:创建折叠 示例:zf 3j    #本行及以下3 ...

  3. 转载一篇ios7的新API文章

    不看不知道,一看吓一跳啊!请看看吧,http://leafduo.com/articles/2013/09/28/ios7/

  4. android 初探

    2014年7月27日 15:02:57 附: android 官方培训课程中文版 //官方简单的入门教程, 每个大类中只介绍了几个知识点, 可以快速搭建一个hello world android 开发 ...

  5. Java for LeetCode 144 Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  6. css样式,层叠顺序属性z-index

    在做项目的时候,居然单击后显示的顺序一直被别的li标签压着,最后终于找到了,是css的z-index属性赋值了,值越大,显示的层就越高 详情推荐百度百科:z-index z-index是针对网页显示中 ...

  7. linux svn客户端 常用命令

    查看文件或者目录状态: [root@v01 ~]# svn status online/ #正常情况下没显示 [root@v01 ~]# svn status online/ #如果有变动会有如下显示 ...

  8. Codeigniter:如何写一个好的Model

    本文是关于在Code Igniter PHP MVC框架中如何编写Model方法. CRUD 方法 CRUD 是Create, Retrieve, Update, and Delete的缩写. 这些是 ...

  9. (转)云存储:阿里云OSS 、又拍云和 七牛 的比较

    阿里OSS:好处就是,那是一套完整的体系,存储,数据库,CDN,服务器,阿里都可以给你全包.缺点,费用对于没有盈利的网站来说太高了,好像定位就是给那些高端客户使用的,而且CDN,OSS的流量是分开收费 ...

  10. mysql忘记密码修改方法

    1.干掉mysqld进程 kill -TERM mysqld 2.使用下面命令启动mysqld /usr/bin/mysqld_safe --skip-grant-tables & 3.新开一 ...