Modular Inverse 【欧几里得求 最小逆元】
来源:互联网 发布:linux百度翻译 编辑:程序博客网 时间:2024/05/22 00:53
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
看代码吧
#include<stdio.h>#include<math.h>#include<iostream>using namespace std;const int MAXN =1E6;void exgcd(int a,int b,int &x,int &y,int &d){ if(b==0) { d=a; x=1;y=0;} else { exgcd(b,a%b,y,x,d); y-=(a/b)*x; }}int chi(int a,int n){ int x,y,d; exgcd(a,n,x,y,d); if(d!=1) return -1; x=x%n; if(x<=0) x+=n; return x;}int main(){ int t;cin>>t; while(t--){ int a,m; cin>>a>>m; int ans=chi(a,m); if(ans==-1) puts("Not Exist"); else cout<<ans<<endl; }}
阅读全文
0 0
- Modular Inverse 【欧几里得求 最小逆元】
- zoj 3609 Modular Inverse 求最小逆元 裸的的扩展欧几里得
- ZOJ 3609 Modular Inverse(求最小乘法逆元)
- ZOJ(Modular Inverse)——最小乘法逆元
- ZOJ 3609 Modular Inverse(扩展欧几里德求乘法逆元)
- 扩展欧几里得-逆元 浙江2012年省赛J题 Modular Inverse
- 【逆元】ZOJ 3609 Modular Inverse
- 【数学 逆元】zoj 3609 Modular Inverse
- ZOJ 3609 Modular Inverse(乘法逆元)
- zoj 3609 Modular Inverse(逆元)
- ZOJ 3606 Modular Inverse【乘法逆元】
- (扩展欧几里得)Modular Inverse--Zoj
- ZOJ3609 Modular Inverse (扩展欧几里得)
- ZOJ - 3609 —— Modular Inverse 【乘法逆,扩展欧几里得】
- ZOJ-3609(Modular Inverse )(拓展欧几里得)
- ZOJ 3609 Modular Inverse 扩展欧几里得
- 浙江省省赛[扩展欧几里得] J Modular Inverse
- Modular Inverse
- 冒泡排序和二分法
- 关于凸包——Graham扫描法
- session,application,out
- Basic Structure 基本结构:
- hdu 5723 Abandoned country
- Modular Inverse 【欧几里得求 最小逆元】
- 常见报错
- 图片的放大和缩小
- TCP/IP 详解卷一学习笔记(四):TCP 连接建立与终止
- QT一些error(C1057、LNK2019、C2146、C3861)的解决
- 通过jQuery Ajax使用FormData对象上传文件
- leetcode 125. Valid Palindrome | 回文string
- 通向架构师的道路(第六天)之漫谈基于数据库的权限系统的设计
- 消息队列的pull与push模式理解