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. The Bottom of a Graph(tarjan + 缩点)

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9139   Accepted:  ...

  2. A+B Again(在某个数中找大于m的最小约数)

    A+B Again Accepted : 15   Submit : 243 Time Limit : 1000 MS   Memory Limit : 65536 KB  题目描述 上次趣味赛小明的 ...

  3. CUDA中的Toolkit

    CUDA Toolkit是什么? 对于使用 C 语言和 C++ 来开发 GPU 加速应用程序的开发者来说,NVIDIA CUDA Toolkit 可提供一个综合的开发环境.CUDA Toolkit 包 ...

  4. HTML前端--各种小案例

    掬一捧清水,放逐在江河,融入流逝的岁月,将心洗净; 捻一缕心香,遥寄在云端,在最深的红尘里重逢,将心揉碎; 望一程山水,徘徊在月下,在相思渡口苦守寒冬,将心落寞. 案例一: 隐藏扩展域,并去掉afte ...

  5. Android 中 设置TextView垂直滚动

    布局文件 android:scrollbars="vertical" android:singleLine="false" 代码文件 ctl_tv_conten ...

  6. docker的四种网络模式

    /* 1. host模式 : docker run 使用 --net=host指定 docker使用的网络实际上和宿主机一样 2. container模式: 使用 --net=container:co ...

  7. 【云计算】Docker Nginx示例

    使用数据卷容器,配置Nginx Docker作为静态文件服务器 . 该方法是直接使用命令行,当然也可使用Dockerfile文件进行创建. 其实,使用docker创建nginx容器是很简单的,但要和数 ...

  8. 【转】Eclipse中查看jar包中的源码

    (简单的方式:通过jd-gui来进行反编译,最简单!,参考我的另一篇博文, 地址:http://www.cnblogs.com/gmq-sh/p/4277991.html) Java Decompil ...

  9. 37.寻找丑数[Ugly numbers]

    [题目] 我们把只包含质因子2.3和5的数称作丑数(Ugly Number),例如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第1500个丑 ...

  10. byte[]和InputStream的相互转换[转载]

    1:byte[]转换为InputStream InputStream sbs = new ByteArrayInputStream(byte[] buf); 2:InputStream转换为Input ...