题目链接: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. JAVA基础——生产者消费者问题

    1.生产者消费者问题:经典案例 生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费 ...

  2. 诊断:ORA-00376 & ORA-01110

    现象: Errors in file /path/of/diag/rdbms/prod/PROD/trace/PROD_ora_13447.trc: ORA-00376: 此时无法读取文件 61 OR ...

  3. 爬虫框架urllib 之(二) --- urllib基础

    urllib 官方文档:https://docs.python.org/zh-cn/3/library/urllib.html urllib介绍 Urllib是python内置的HTTP请求库,是py ...

  4. sort 结构体 正数负数分开排序

    对于结构体排序的一点点记录,之前遇到过结构体排序,个人比较喜欢使用在结构体当中直接重载小于号的方法, 例如说: struct Node{ int index; int del; bool operat ...

  5. PAT 1059. C语言竞赛

    PAT 1059. C语言竞赛 C语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 冠军将赢得一份"神秘大奖"(比如很巨大的一本 ...

  6. 添物不花钱学JavaEE(基础篇)-综述

    JavaEE由一堆基础标准组成.JavaEE开发一般需要如下工具和掌握如下内容: Java环境 Eclipse或其他开发工具 Tomcat容器或类似软件 Jsp/Servlet/Web/JDBC的掌握 ...

  7. 60. Spring Boot写后感【从零开始学Spring Boot】

    从2016年4月15日到2016年7月20日经历长达3个月的时间,[从零开始学习Spring Boot]系列就要告一段落了.国内的各种资源都比较乱或者是copy 来copy去的,错了也不加以修正下,导 ...

  8. oracle10g_v$sqltext之对等视图v$sqltext_with_newlines

    --v$sqltext_with_newlines是v$sqltext等同体,较之易读性强(由sql_address及sql_hash_value唯一确认一个cached cursor中的sql)SQ ...

  9. 转盘抽奖 canvas & 抽奖 H5 源码

    转盘抽奖 canvas https://github.com/givebest/wechat-turntalbe-canvas https://blog.givebest.cn/GB-canvas-t ...

  10. 特种部队(codevs 1427)

    题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达仓库时候,发现 ...