[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 ( ...
随机推荐
- 简单的javascript--test2
插件: jquery, sweetalert (http://t4t5.github.io/sweetalert/ ) 1.index.html <!DOCTYPE HTML> <h ...
- Angular JS中 Promise用法
一.Promise形象讲解A promise不是angular首创的,作为一种编程模式,它出现在1976年,比js还要古老得多.promise全称是 Futures and promises. 而在j ...
- Asp.net using Oracle.DataAccess.dll access oracle 11g 64bit & x86
使用.net访问oracle数据库时一般需要在机器上安装instantclient才能正常连接. 下面介绍一种不用安装instantclient直接引用dll就用.net能连接oracle数据库的方法 ...
- 调用百度地图API的应用混淆后出问题
1 混淆后出问题,程序异常退出 在proguard-project.txt中添加 -libraryjars libs/BaiduLBS_Android.jar -keep class com.baid ...
- jquery 根据年 月设置报表表头
function setTblHeadr(thisTime){ $("#datatable_ajax1 thead").empty(); //获取星期 var weekday=ne ...
- (转载)IOS中UIScrollView的属性和委托方法
http://www.jizhishusheng.com/?p=453 ---- 以下内容来自 UIScrollView 类负责所有基于 UIKit 的滚动操作一.创建 1. CGRect bou ...
- 济南学习D2T2__数学分析题
[问题描述]有N个数,随机选择一段区间,如果这段区间的所有数的平均值在[l,r]中则你比较厉害.求你比较厉害的概率.[输入格式]第一行有三个数N,l,r,含义如上描述.接下来一行有N个数代表每一个数的 ...
- (十一) 一起学 Unix 环境高级编程 (APUE) 之 高级 IO
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- andorid SQLite数据库创建文件
package com.hanqi.application3; import android.content.ContentValues; import android.database.sqlite ...
- vbox进行克隆时时启动eth0时问题
在进行虚拟机克隆时会出现mac地址冲突,在进行网卡启动时,会出现device eth0 does not seem to be present问题,解决方案如下: 问题描述: device eth0 ...