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. SpringBoot25 gradle安装、利用gradle创建SrpingBoot项目

    1 gradle安装 技巧01:gradle依赖JDK或者JRE,而且版本至少时1.7 1.1 下载安装包 到gradle官网下载安装包[PS: 也可以利用命令的方式安装,本案例是利用安装包的方式] ...

  2. oracle 创建表 外键约束

    create table usertable( id int primary key, username ) not null, birthday date, sex ), address ) ); ...

  3. SQLAlchemy(ORM框架)

    SQLAlchemy SQLAlchemy概述 2 3 4 5 6 7 8 9 10 11 12 13 MySQL-Python     mysql+mysqldb://<user>:&l ...

  4. pyinstaller生成exe可执行程序

    1安装 略 2执行 pyinstaller –F test.py 报错: Pyinstaller: cx_Oracle.InterfaceError: Unable to acquire Oracle ...

  5. hdu 4286 (list的reverse时间复杂度为n)

    list 的翻转reverse源码: // 将链表倒置 // 其算法核心是历遍链表, 每次取出一个结点, 并插入到链表起始点 // 历遍完成后链表满足倒置 template <class T, ...

  6. 手打的table

    突然觉得,如果我不上传源码和写篇博客,对不起花在这个破网页2个小时的时间,完全手打,浏览器调效果. 源码如下: a.html: <!DOCTYPE html PUBLIC "-//W3 ...

  7. 跨域问题hbuilder

    1.借助jquery-jsonp插件 $.jsonp({ url: url, data: { 'name': usd, 'passwd': pass }, callbackParameter: &qu ...

  8. Reportng 的测试报告在 Jenkins 中显示不全

    通过Jenkins执行接口测试生成测试报告,用Jenkins的web服务打开html显示不全. 环境: Jenkins版本:1.651.2 Jenkins发布在mac上面 使用的是testng管理测试 ...

  9. [docker]Kubernetes的yaml文件

    yaml是一种专门用来写配置的语言,简洁强大 它的规则: 1.大小写敏感 2.使用缩进表示层级关系,但不支持tab缩进,只支持空格 3.缩进的数量不重要但至少一个空格,只要相同层级使用相同数量的空格即 ...

  10. Alpha冲刺(二)

    Information: 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Details: 组员1(组长)柯奇豪 过去两天完成了哪些任务 学习并配置了ssm框架(用于前后端交 ...