[ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]
Description
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 toax≡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
题目大意:求a关于模m的逆。
解题思路:1>扩展欧几里得算法:找出一对整数对(x,y),使得ax+by=gcd(a,b).
2>设a,b,c为任意整数.若方程ax+by=c的一组整数解为(x,y),则它的任意解为(x+k*b',y+k*a'),其中a'=a/gcd(a,b),b'=b/gcd(a,b),k任意整数.
3>模线性方程:输入正整数:a,b,n,解方程ax≡b(mod n)即:a-b是n的整数倍即:ax-b=ny.
4>ax≡1 (mod m)等价于:ax%m==1%m 也等价于:ax-my=1是否有整数解且求出满足条件的最小整数x。扩展欧几里得算法1必须是gcd(a,m)的倍数,所以a和n互素即:gcd(a,m)=1才会有解,在该条件下有唯一解。
#include<iostream>
#include<string.h>
#include<cstring>
#include<string>
using namespace std;
void gcd(int a,int b,int& d,int& x,int& y){
if(!b){
d=a;x=;y=;
}else{
gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}//扩展欧几里得算法,a,b,是输入量
//d为gcd(a,b),x,y为ax+by=gcd(a,b)的一组整数解
int main(){
int T;cin>>T;
while(T--){
int a,m,d,x,y;
cin>>a>>m;
gcd(a,m,d,x,y);
if(d!=)cout<<"Not Exist\n";
else{//根据一组解求满足条件的x
if(x>){
while(x>)x-=m;
x+=m;
}else if(x<){
while(x<)x+=m;
}else x+=m;
cout<<x<<'\n';
}
}return ;
}
[ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]的更多相关文章
- 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 ...
- Modular Inverse(zoj3609+欧几里德)
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 ...
- 【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 ...
- Modular Inverse (拓展欧几里得求逆元)
The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x ( ...
随机推荐
- js数组合并
// 第一种 var mergeTo = [4,5,6], mergeFrom = [7,8,9]; mergeTo = mergeTo.concat(mergeFrom); mergeTo; // ...
- 《C++必知必会》学习笔记
转载:http://dsqiu.iteye.com/blog/1734640 条款一 数据抽象 抽象数据设计遵循步骤:(1)为类型取一个描述性的名字.(2)列出类型所能执行的操作,不要忘了初始化(构造 ...
- 自定义底部tab
public class MainActivity extends TabActivity implements OnCheckedChangeListener { private RadioGrou ...
- Python 黑魔法 --- 描述器(descriptor)
Python 黑魔法---描述器(descriptor) Python黑魔法,前面已经介绍了两个魔法,装饰器和迭代器,通常还有个生成器.生成器固然也是一个很优雅的魔法.生成器更像是函数的行为.而连接类 ...
- 自定义Dialog之信息提示
对话框对于应用也是必不可少的一个组件,在Android中也不例外,对话框对于一些提示重要信息,或者一些需要用户额外交互的一些内容很有帮助. 自定义Dialog步骤: 1.主要创建Java类,并继承Di ...
- Search and Replace
function myReplace(str, before, after) { //return str; if(before[0] === before[0].toUpperCase()){ af ...
- POJ 2352Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42898 Accepted: 18664 Descripti ...
- 解决Failed to load class "org.slf4j.impl.StaticLoggerBinder"
Hibernate使用SLF4J API记录日志,所以在Hibernate的lib中,不再提供Log4J的包,而大部分框架依然使用Log4J记录日志,这样导致了兼容性问题. 解决办法,两步: 一.在编 ...
- IntelliJIDEA 14创建Maven管理的Java Web项目
1.新建项目,选择Maven,点击Next继续. 接着输入项目名 接着直接点击Finish即可 下图就是创建完毕后的Maven项目,双击pom.xml查看POM文件内容,可以自行添加Maven的依赖. ...
- Java将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
java.io 类 BufferedWriter java.lang.Object java.io.Writer java.io.BufferedWriter BufferedWriter publi ...