题目链接: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

  1. 3
  2. 3 11
  3. 4 12
  4. 5 13

Sample Output

  1. 4
  2. Not Exist
  3. 8

References

代码例如以下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. typedef long long LL;
  5.  
  6. LL exgcd(LL a,LL b,LL &x,LL &y)
  7. {
  8. if(b == 0)
  9. {
  10. x = 1;
  11. y = 0;
  12. return a;
  13. }
  14. else
  15. {
  16. LL r = exgcd(b,a%b,x,y);
  17. LL t = x;
  18. x = y;
  19. y = t-a/b*y;
  20. return r;
  21. }
  22. }
  23.  
  24. LL cal(LL a, LL b, LL c)
  25. {
  26. LL x, y;
  27. LL tt = exgcd(a, b, x, y);
  28. if(c%tt)//无整数解
  29. {
  30. return -1;
  31. }
  32. x*=c/tt;
  33. b/=tt;
  34. if(b<0)
  35. b=-b;
  36. LL ans=x%b;
  37. if(ans<=0)
  38. ans+=b;
  39. return ans;
  40. }
  41.  
  42. int main()
  43. {
  44. LL a, b, t;
  45. scanf("%lld",&t);
  46. while(t--)
  47. {
  48. scanf("%lld%lld",&a,&b);
  49. LL ans = cal(a, b, 1);
  50. if(ans == -1)
  51. {
  52. printf("Not Exist\n");
  53. continue;
  54. }
  55. printf("%lld\n",ans);
  56. }
  57. return 0;
  58. }

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. selenium抓取动态网页数据

    1.selenium抓取动态网页数据基础介绍 1.1 什么是AJAX AJAX(Asynchronouse JavaScript And XML:异步JavaScript和XML)通过在后台与服务器进 ...

  2. oracle分配权限 学习笔记--转载

    在全局数据库ORCL下创建一个用户首先在开始-->运行——>sqlplus,然后输入 sys/change_on_install as sysdba 以sys权限登陆进去 然后可以进行操作 ...

  3. 「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓

    # 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花 ...

  4. 关于latch: cache buffers chains的sql优化

    前段时间,优化了一些耗buffer比较多的sql,但是CPU使用率还是没下来 . 查看操作系统CPU使用率 查看awr,发现又有一条超级耗性能的sql冒出来了. 该SQL每次执行耗费3e多个buffe ...

  5. Go:struct

    一.使用方式 方式3和方式4返回的是:结构体指针,编译器底层对 p.Name 做了转化 *(p).Name,方便程序员使用. type Person struct { Name string Age ...

  6. 如何系统学习并且掌握JavaScript

  7. shell脚本、if语句、for循环语句

    shell在shell脚本中,如果用户不输入东西,系统不自动退出,this is a bug!文件测试语句:-d -f -r -w -x -e逻辑测试语句:“&&”与(同时满足) “| ...

  8. MySQL-----备份(转储)

    备份: **备份数据表结构+数据** mysqldump -u root 要备份的数据库表名 > 要备份的数据的备份名(这里也可以指定路径) -p **备份数据表结构** mysqldump - ...

  9. 多.h项目出现的问题:使用了预编译头依然出现error LNK2005:***obj已在***obj中定义与c++ error C2011: “xxx”:“class”类型重定义解决办法

    使用了预编译头依然出现error LNK2005:***obj已在***obj中定义 造成该问题的可能性比较多,本人将在今后遇到时添加进来,今天先放出本人遇到的一种情况. 多重包含含有变量定义的.h文 ...

  10. ASP.NET获取客户端IP及MAC地址

    朋友最近问如何获取客户端IP及MAC地址,一直想把这段给整理一下,契机来了:下边分为了C#后台获取的方法和前台Javascript(调用ActiveX)获取的方法,大家如果有好的方法一起讨论撒O(∩_ ...