Modular Inverse (拓展欧几里得求逆元)
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 (拓展欧几里得求逆元)的更多相关文章
- ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)
gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...
- hdu_1576A/B(扩展欧几里得求逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others) Me ...
- POJ 1061 青蛙的约会(拓展欧几里得求同余方程,解ax+by=c)
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 122871 Accepted: 26147 Descript ...
- ZOJ 3593 One Person Game(拓展欧几里得求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- 拓展欧几里得求 ax + by = c的通解(a >=0, b >= 0)
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> ...
- 扩展欧几里得模板&逆元求法
拓展欧几里得: 当 gcd ( a , b )= d 时,求绝对值和最小的 x , y 使得 x * a + y * b = d : d = gcd ( a , b ) = gcd ( b , a m ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- Looooops(求解同余方程、同余方程用法)【拓展欧几里得】
Looooops(点击) A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
随机推荐
- 新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项
新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项. 在弹出的窗口中,下拉滚动条找到Server,并单击next按钮. 在弹出的窗口中,找到第一个A ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- spring框架 事务 注解配置方式
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...
- vs2017不是完全支持c99
1.比如c99里面有一个特性, int count[]={0,[5]=7,9,10} 这种在VS2017里面是编译不通过的.; 2.c99有一个变长数组的概念(VLA),但是vs2017不支持.
- R list frame, matrix
list是个筐,什么都可以往里装,包括它自身.数据框是个二维数组,同列必须同类型,行随意.
- spark源码阅读之network(2)
在上节的解读中发现spark的源码中大量使用netty的buffer部分的api,该节将看到netty核心的一些api,比如channel: 在Netty里,Channel是通讯的载体(网络套接字或组 ...
- 解析json的方法
解析json的两种方法:JS中的eval().JSON.parse eval不仅解析内容还会解析其中的方法,JSON.parse解析更安全.JSONLint可校验json的错误.
- css总结11:css的overflow问题
1 排版时经常遇到块级元素内容overflow,怎么妥当处理是一个关键. overflow的常用属性: 代码: <!DOCTYPE html><html lang="en ...
- [转]-webkit-overflow-scrolling:touch的应用
-webkit-overflow-scrolling 用来控制元素在移动设备上是否使用滚动回弹效果. 在移动端上,在你用overflow-y:scorll属性的时候,你会发现滚动的效果很木,很慢,这时 ...
- vs2015编译ffmpeg 出现错误rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 ___iob_func
vs2015编译ffmpeg(版本3.0.2)引用外部库文件librtmp出现以下错误: rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 __imp__st ...