The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x (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

今晚实在是想吐槽一下这个题,很无奈啊,本来想四题签到跑路,被这个题卡了2个小时,首先暴力枚举得没过,然后翻板子在拓展欧几里的找到了类似得题目,把板子搞上,疯狂wa,wa到自闭,结果是一组m为1时的特例为1,程序输出为0,还是太菜了,真************(疯狂喷自己)

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int extend_gcd(int a,int b,int &x,int &y)
{
if(a==0&&b==0)
{
return -1;
}
if(b==0)
{
x=1;y=0;
return a;
}
int d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int mod_reverse(int a,int n)
{
int x,y;
int d=extend_gcd(a,n,x,y);
if(d==1)return(x%n+n)%n;
else return -1;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int a,n;
cin>>a>>n;
if(n==1)
{
cout<<1<<endl;
continue;
}
if(__gcd(a,n)!=1)
{
cout<<"Not Exist"<<endl;
continue;
}
cout<<mod_reverse(a,n)<<endl;
} return 0;
}

Modular Inverse (拓展欧几里得求逆元)的更多相关文章

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

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

  2. gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)

    gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...

  3. hdu_1576A/B(扩展欧几里得求逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others)    Me ...

  4. POJ 1061 青蛙的约会(拓展欧几里得求同余方程,解ax+by=c)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 122871   Accepted: 26147 Descript ...

  5. ZOJ 3593 One Person Game(拓展欧几里得求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  6. 拓展欧几里得求 ax + by = c的通解(a >=0, b >= 0)

    #include <iostream> #include <cstdio> #include <algorithm> #include <vector> ...

  7. 扩展欧几里得模板&逆元求法

    拓展欧几里得: 当 gcd ( a , b )= d 时,求绝对值和最小的 x , y 使得 x * a + y * b = d : d = gcd ( a , b ) = gcd ( b , a m ...

  8. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  9. Looooops(求解同余方程、同余方程用法)【拓展欧几里得】

    Looooops(点击) A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...

随机推荐

  1. Android 创建项目出现No resource found that matches the given name Theme.AppCompat.Light

    关于为何出现No resource found that matches the given name ‘Theme.AppCompat.Light’的原因 这边博客已经写的很清楚了 大家可以参考一下 ...

  2. 用Jquery实现修改页面selecte标签的默认选择

    在WEB开发中,最基础的也是用的最多的就是数据库的增删改查,修改往往以为的小部分的改动,所以我们往往是在表单中填充以前的内容然后显示给用户进行修改操作. 在填充默认内容的时候对于input标签我们往往 ...

  3. 27-拓扑排序-poj1094

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  4. csv、txt读写及模式介绍

    1读写模式 r以读方式打开文件,可读取文件信息 w已写方式打开文件,可向文件写入信息.如文件存在,则清空,再写入 a以追加模式打开文件,打开文件可指针移至末尾,文件不存在则创建 r+以读写方式打开文件 ...

  5. cakephp重写配置

    开启重新: (1)开启服务器的mod_rewrite模块 (2)注释掉app/ConfigScore.php中的 Configure::write('App.baseUrl', env('SCRIPT ...

  6. JMS-消息中间件的应用01-基本概念-来自慕课学习-新手学习

    什么是JMS? Java消息服务(Java Message Service),即JMS,是一个java平台中关于面向消息中间件的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. ...

  7. 十进制--->二进制(利用C++栈功能)

    原创 十进制转二进制很简单,其中用到C++的栈功能就能更加方便! stack<int> s; //栈的定义,s已经被定义为一个栈 s.push(); //将20入栈 s.push(); s ...

  8. 六、Note开发工具Visual Studio Code下载安装以及Visual Studio Code的使用

    专业的人干专业的事,我们搞Node总不能真的使用文本编辑器傻乎乎的搞吧,文本编辑器来开发Node程序,效率太低,运行Node程序还需要在命令行单独敲命令.如果还需要调试程序,就更加麻烦了.所以我们需要 ...

  9. 函数返回值string与返回值bool区别------c++程序设计原理与实践(进阶篇)

    为什么find_from_addr()和find_subject()如此不同?比如,find_from_addr()返回bool值,而find_subject()返回string.原因在于我们想说明: ...

  10. Linux中,关闭selinux

    首先我们可以用命令来查看selinux的状态getenforce 这个命令可以查看到selinux的状态,当前可以看到是关闭状态的. 还有一个命令也可以查看出selinux的状态.sestatus - ...