[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 ( ...
随机推荐
- JSF 嵌套
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com ...
- 2015年可用的TRACKER服务器大全
udp://tracker.openbittorrent.com:80/announceudp://tracker.publicbt.com:80/announcehttp://pubt.net:27 ...
- lucene prefixQuery
prefixQuery是lucene的一个前缀匹配的搜索功能,但是它的前缀匹配不是完全的前缀匹配,的query生成是:易* 但是能把所有带有易字的都搜到... 下面是实验:出现了交易...
- 技术英文单词贴--W
W widget 小工具,小部件
- 修改navigationbar右侧按钮离屏幕边缘位置
先上代码 UIButton *settingBtn = [Utils creatCustomButtonWithFrame:CGRectMake(, , , ) btnTitle: titleColo ...
- MySQL表分区
MySQL的表分区 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了.如:某用户表的记录超过了600万条,那么就可以根据入库日期将表分区,也可以 ...
- js之正则1
在看jquery的源码时,看到对$对象的init入口对参数解析时,正则的迷惑. 疑惑点:z = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/ a = ...
- Ubuntu 12.04(32位)安装Oracle 11g(32位)
安装过程(主要过程就直接copy别人的教程了)及问题: 1.将系统更新到最新: sudo apt-get updatesudo apt-get dist-upgrade 2. 如果使用的Ubuntu不 ...
- Android打包签名
Ⅰ.用jdk和sdk自带工具打包签名 a.把jdk下的keytool.exe和jarsigner.exe所在目录(两个工具在同一目录) 添加到环境变量path 1)新建环境变量package,pack ...
- 新建MMS草稿保存后,再进入草稿修改收件人,退出到会话界面,会显示两条草稿
分析原因:新建彩信草稿,保存后,再打开草稿,修改收件人,再保存会产生新的threadId, 而之前保存的草稿theadId还存在,导致此问题. (必现. 解决方法:将原来的Thread_id和新的收件 ...