Modular Inverse(zoj3609+欧几里德)
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-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
Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
首先我来回顾下欧几里德的几个定理,有助于理解这道题;
定理一:如果d = gcd(a, b),则必能找到正的或负的整数k和l,使 d = a*x+ b*y。
定理二:若gcd(a, b) = 1,则方程ax ≡ c (mod b)在[0, b-1]上有唯一解。
定理三:若gcd(a, b) = d,则方程ax ≡ c (mod b)在[0, b/d - 1]上有唯一解。
转载请注明出处:寻找&星空の孩子
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712
对于ax+by=1; 即ax=1(mod b) 当且仅当gcd(a,b)!=1 的时候,无解!
#include<stdio.h> void exgcd(int a,int b,int &d,int &x,int &y)
{
if(!b){d=a;x=;y=;}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
int main()
{
int T,a,m;
scanf("%d",&T);
while(T--)
{
int d,x,y;
scanf("%d%d",&a,&m);
exgcd(a,m,d,x,y);
if(d==)
{
while(x<=)
{
x+=m/d;
}
printf("%d\n",x);
}
else
printf("Not Exist\n");
}
return ;
}
Modular Inverse(zoj3609+欧几里德)的更多相关文章
- 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 ...
- 寒假 D3 D Modular Inverse
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB ...
- zjuoj 3609 Modular Inverse
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...
- ZOJ——3609 Modular Inverse
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- ZOJ 3609 Modular Inverse(扩展欧几里德)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...
- [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]
Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...
- 【ZOJ 3609】Modular Inverse 最小乘法逆元
The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x ...
- B - Modular Inverse
The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x ...
随机推荐
- POJ2455 Secret Milking Machine
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12324 Accepted ...
- 「SDOI2016」储能表(数位dp)
「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...
- HTTP 协议基础概念和报文结构
基础概念 1.WWW(World Wide Web,万维网)构建技术有3项: (1)把SGML(Standard Generalized Markup Language,标准通用标记语言)作为页面的文 ...
- request和response简介
Tomcat收到客户端的http请求,会针对每一次请求,分别创建一个代表请求的request对象.和代表响应的response对象. 既然request对象代表http请求,那么我们获取浏览器提交过来 ...
- 【LeetCode】9. 回文数
题目 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1:输入: 121 输出: true 示例 2:输入: -121 输出: false 解释: 从左 ...
- 递归查询区域信息及子区域到advTree
效果: DataTable dtArea = new DataTable(); private void Form1_Load(object sender, EventArgs e) { Node n ...
- oracle中常见的查询操作
普通查询:select * from t; 去除重复值:select distinct f1,f2 from t; between用法:select * from t where f1 not/bet ...
- Linux学习笔记之八————vim编辑器常用命令总结
<1>从命令行模式到插入模式 i :在光标前插入 a :在光标后插入 I :在光标所处在的行的行首 A :在光标所处在的行的末尾 o :在光标所处在的行的下一行 行首 O :在光标所 ...
- Apache Commons Digester 二(规则模块绑定-RulesModule、异步解析-asyncParse、xml变量Substitutor、带参构造方法)
前言 上一篇对Digester做了基本介绍,也已经了解了Digester的基本使用方法,接下来将继续学习其相关特性,本篇主要涉及以下几个内容: 规则模块绑定,通过定义一个RulesModule接口实现 ...
- Java NIO中的通道Channel(一)通道基础
什么是通道Channel 这个说实话挺难定义的,有点抽象,不过我们可以根据它的用途来理解: 通道主要用于传输数据,从缓冲区的一侧传到另一侧的实体(如文件.套接字...),反之亦然: 通道是访问IO服务 ...