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
题解:求最小正整数解,其实吧,x的通解是x0+b/gcd*t,由于t是整数,那么ans=x0+b/gcd*t=x0 mod b=x0%b;因为ans要是正整数的,
所以当b/gcd是负的时候,就等于绝对值就好了,因为还有t啊,当x0%b负时,加上一个b;就妥了;因为ans=(x0+b)%b;
代码:
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
d=a;
x=;
y=;
}
else{
e_gcd(b,a%b,d,x,y);
LL temp=x;
x=y;
y=temp-a/b*y;
}
}
LL cal(int a,int b,int c){
LL x,y,d;
e_gcd(a,b,d,x,y);
if(c%d!=)return -;//ax+by=c/(c/gcd);
x*=c/d;
b/=d;//因为x的通解是x0+(b/gcd)t;
if(b<)b=-b;
LL ans=x%b;
if(ans<=)ans+=b;
return ans;
}
int main(){
LL T,a,b,d,x,y;
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&a,&b);
LL ans=cal(a,b,);
if(ans==-)puts("Not Exist");
else printf("%lld\n",ans);
}
return ;
}

题上数据比较水,数据范围1000,暴力一下就可以了:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
int main(){
int T,a,m;
scanf("%d",&T);
while(T--){//(1-ax)%m;
scanf("%d%d",&a,&m);
int flot=;
for(int x=;x<=;x++){
if((-a*x)%m==){
flot=;
printf("%d\n",x);
break;
}
}
if(flot)continue;
puts("Not Exist");
}
return ;
}

Modular Inverse(模逆元,扩展欧几里德)的更多相关文章

  1. 51Nod 1256 求乘法逆元--扩展欧几里德

    #include<stdio.h> int exgcd(int a,int b,int &x,int &y) { ) { x=; y=; return a; } int r ...

  2. ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)

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

  3. CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元

    题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...

  4. POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))

    d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...

  5. Modular Inverse(zoj3609+欧几里德)

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

  6. HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))

    Invoker Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 122768/62768K (Java/Other) Total Subm ...

  7. 公钥密码之RSA密码算法扩展欧几里德求逆元!!

    扩展欧几里得求逆元 实话说这个算法如果手推的话问题不大,无非就是辗转相除法的逆过程,还有一种就是利用扩展欧几里德算法,学信安数学基础的时候问题不大,但现在几乎都忘了,刷题的时候也是用kuangbin博 ...

  8. POJ-1061青蛙的约会,扩展欧几里德求逆元!

                                                               青蛙的约会 以前不止一次看过这个题,但都没有去补..好吧,现在慢慢来做. 友情提示 ...

  9. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

随机推荐

  1. thinkPHP的常用配置项

    'URL_PATHINFO_DEPR'=>'-',//修改URL的分隔符 'TMPL_L_DELIM'=>'<{', //修改左定界符 'TMPL_R_DELIM'=>'}&g ...

  2. easyui 验证控件 tooltip message显示位置

    找了半天才发现是这个属性在控制,tipPosition:'left',官网那个demo,误人子弟.

  3. ETL工具之ODI

    ETL工具之ODI         到目前为止,Oracle的ETL工具包括两种,分别是Oracle Warehouse Builder(OWB)和Oracle Data Integrator(ODI ...

  4. 美版nexus 5 LG D820才支持CDMA,国际版LG D821不支持

    我们都知道nexus 5其实是有两个不同的版本的,分别是LG D820和LG D821,它们在几乎所有的配置和外观上都没有任何的区别,主要区别在通讯模块上,一个支持GSM/CDMA/WCDMA/LTE ...

  5. kafka初探

    http://www.infoq.com/cn/articles/kafka-analysis-part-1

  6. 微信开放框架-UCToo

    UCToo是一套简单,易用,开源的微信增值应用开发框架,帮助用户快捷的实现微信公众平台的个性化定制功能. http://www.uctoo.com/

  7. CCNP路由实验(2) -- OSPF

    OSPF作为一种内部网关协议(IGP),用于在同一个AS中的路由器之间交换路由信息.OSPF的特性如下:1.可适应大规模网络2.收敛速度快3.无路由环路4.支持VLSM和CIDR5.支持等价路由6.支 ...

  8. HDU2546:饭卡(01背包)

    HDU2546:饭卡 http://acm.hdu.edu.cn/showproblem.php?pid=2546 当我们遇到问题选择物体的价值和顺序相关时就需要,排完序后对其01处理.这题因为当我们 ...

  9. Python TypeError: not enough arguments for format string

    今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...

  10. 浮点数(double)的优势

    用 double 处理大数据 , 即使字符串位数超百位,同样可以用 sscanf 或 atof 来处理,却不会变成负数,原因是浮点数可以用指数表示,但精度会丢失,下面介绍利用这个性质解决的一些问题: ...