ZOJ 3609 Modular Inverse(扩展欧几里德)
题目链接: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-1≡x (mod
. This is equivalent to
m)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(扩展欧几里德)的更多相关文章
- ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- ZOJ——3609 Modular Inverse
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- ZOJ 3609 Modular Inverse(扩展欧几里得)题解
题意:求乘法逆元最小正正数解 思路:a*x≡1(mod m),则称x 是 a 关于 m 的乘法逆元,可以通过解a*x + m*y = 1解得x.那么通过EXGcd得到特解x1,最小正解x1 = x1 ...
- ZOJ 3609 Modular Inverse
点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...
- zjuoj 3609 Modular Inverse
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...
- 【ZOJ】3609 Modular Inverse
1. 题目描述求乘法逆元. 2. 基本思路利用扩展gcd求逆元,模板题目. 3. 代码 /* 3609 */ #include <iostream> #include <sstrea ...
- Modular Inverse(模逆元,扩展欧几里德)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- Modular Inverse(zoj3609+欧几里德)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- (扩展欧几里德算法)zzuoj 10402: C.机器人
10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...
随机推荐
- selenium抓取动态网页数据
1.selenium抓取动态网页数据基础介绍 1.1 什么是AJAX AJAX(Asynchronouse JavaScript And XML:异步JavaScript和XML)通过在后台与服务器进 ...
- oracle分配权限 学习笔记--转载
在全局数据库ORCL下创建一个用户首先在开始-->运行——>sqlplus,然后输入 sys/change_on_install as sysdba 以sys权限登陆进去 然后可以进行操作 ...
- 「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓
# 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花 ...
- 关于latch: cache buffers chains的sql优化
前段时间,优化了一些耗buffer比较多的sql,但是CPU使用率还是没下来 . 查看操作系统CPU使用率 查看awr,发现又有一条超级耗性能的sql冒出来了. 该SQL每次执行耗费3e多个buffe ...
- Go:struct
一.使用方式 方式3和方式4返回的是:结构体指针,编译器底层对 p.Name 做了转化 *(p).Name,方便程序员使用. type Person struct { Name string Age ...
- 如何系统学习并且掌握JavaScript
- shell脚本、if语句、for循环语句
shell在shell脚本中,如果用户不输入东西,系统不自动退出,this is a bug!文件测试语句:-d -f -r -w -x -e逻辑测试语句:“&&”与(同时满足) “| ...
- MySQL-----备份(转储)
备份: **备份数据表结构+数据** mysqldump -u root 要备份的数据库表名 > 要备份的数据的备份名(这里也可以指定路径) -p **备份数据表结构** mysqldump - ...
- 多.h项目出现的问题:使用了预编译头依然出现error LNK2005:***obj已在***obj中定义与c++ error C2011: “xxx”:“class”类型重定义解决办法
使用了预编译头依然出现error LNK2005:***obj已在***obj中定义 造成该问题的可能性比较多,本人将在今后遇到时添加进来,今天先放出本人遇到的一种情况. 多重包含含有变量定义的.h文 ...
- ASP.NET获取客户端IP及MAC地址
朋友最近问如何获取客户端IP及MAC地址,一直想把这段给整理一下,契机来了:下边分为了C#后台获取的方法和前台Javascript(调用ActiveX)获取的方法,大家如果有好的方法一起讨论撒O(∩_ ...