Modular Inverse

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
Submit    Status

Copyright @ 2001-2017, Zhejiang University ACM/ICPC Team, All rights reserved.

思路:

  裸地乘法逆元
但是让我做的可谓是错误百出。。。。(交了差不多10遍吧。。。)
错误:
1.多组数据
2。最小正整数,所以当求出来的x为0时也要加abs(b)
3.最小 那就%b
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int t,a,b,x,y,gcd;
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
int exgcd(int a,int b,int &x1,int &y1)
{
    )
    {
        x=,y=;
        return a;
    }
    int r=exgcd(b,a%b,x1,y1),tmp;
    tmp=x1,x1=y1,y1=tmp-a/b*y1;
    return r;
}
int main()
{
    while(scanf("%d",&t)!=EOF)
    while(t--)
    {
        a=read(),b=read();
        gcd=exgcd(a,b,x,y);
        x%=b;
        ) printf("Not Exist\n");
        else  {while(x<=0) x+=abs(b); printf("%d\n",x);}
    }
    ;
}

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(扩展欧几里德)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...

  3. ZOJ 3609 Modular Inverse

    点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...

  4. ZOJ 3609 Modular Inverse(扩展欧几里得)题解

    题意:求乘法逆元最小正正数解 思路:a*x≡1(mod m),则称x 是 a 关于 m 的乘法逆元,可以通过解a*x + m*y = 1解得x.那么通过EXGcd得到特解x1,最小正解x1 = x1 ...

  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. 寒假 D3 D Modular Inverse

    Modular Inverse Time Limit: 2 Seconds                                     Memory Limit: 65536 KB     ...

  8. Modular Inverse(模逆元,扩展欧几里德)

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

  9. ZOJ 3609 求逆元

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

随机推荐

  1. 【BZOJ4059】Non-boring sequences(分析时间复杂度)

    题目: BZOJ4059 分析: 想了半天没什么想法,百度到一个神仙做法-- 设原数列为 \(a\),对于每一个 \(i\) 求出前一个和后一个和 \(a_i\) 相等的位置 \(pre[i]\) 和 ...

  2. 转 awr自动收集脚本

    1. remote get awr report #!/usr/bin/ksh ####sample: sh awr.sh 20170515 20170516 AWR ### default it w ...

  3. CSS 样式的优先级小结

    1. 同一元素引用了多个样式时,排在后面的样式属性的优先级高 例如,下面的 div,同时引用了 [.default] 和 [.user] 中的样式,其中 [.user] 样式中的 width 属性会替 ...

  4. Hadoop的数据采集框架

    问题导读: Hadoop数据采集框架都有哪些? Hadoop数据采集框架异同及适用场景? Hadoop提供了一个高度容错的分布式存储系统,帮助我们实现集中式的数据分析和数据共享.在日常应用中我们比如要 ...

  5. CF817A Treasure Hunt

    思路: 起点(x1, y1),终点(x2, y2),步长(dx, -dy),(dx, dy),(-dx, -dy),(-dx, dy).只要满足abs(x1 - x2) % dx == 0 并且 ab ...

  6. 第3章 DOM

    1.节点,dom有3种节点,元素节点,文本节点,属性节点 2.元素节点是dom的原子,所有的属性节点和文本节点都被元素包含,但并不是所有的元素都包含他们 3.继承,节点树上的元素将继承父元素的样式和属 ...

  7. 配置Tomcat && Http简介

    WEB环境搭建 1. Web服务器 Web服务器主要用来接收客户端发送的请求和响应客户端请求. 作为JavaWeb程序来说,还需要有Servet容器,容器的主要作用就是调用java程序处理用户发送的请 ...

  8. UVM基础之-------uvm factory机制override<博>

    override功能是UVM中一个比较重要的功能,这个功能也是在factory里面实现的,我们会在env或者具体的case中使用override功能. class case_x extends bas ...

  9. Microsoft SQL Server学习(七)--函数视图

    系统函数 视图 索引 1.系统函数 (1) ()数学函数 Abs() 绝对值 Floor() 向下取整 Ceiling() 向上取整 Sin() 返回指定角度(以弧度为单位)的三角正弦值 Pi() 圆 ...

  10. canvas一周一练 -- canvas绘制立体文字(2)

    运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...