Modular Inverse(模逆元,扩展欧几里德)
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
题解:求最小正整数解,其实吧,x的通解是x0+b/gcd*t,由于t是整数,那么ans=x0+b/gcd*t=x0 mod b=x0%b;因为ans要是正整数的,
所以当b/gcd是负的时候,就等于绝对值就好了,因为还有t啊,当x0%b负时,加上一个b;就妥了;因为ans=(x0+b)%b;
代码:
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- using namespace std;
- const int INF=0x3f3f3f3f;
- typedef long long LL;
- void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){
- if(!b){
- d=a;
- x=;
- y=;
- }
- else{
- e_gcd(b,a%b,d,x,y);
- LL temp=x;
- x=y;
- y=temp-a/b*y;
- }
- }
- LL cal(int a,int b,int c){
- LL x,y,d;
- e_gcd(a,b,d,x,y);
- if(c%d!=)return -;//ax+by=c/(c/gcd);
- x*=c/d;
- b/=d;//因为x的通解是x0+(b/gcd)t;
- if(b<)b=-b;
- LL ans=x%b;
- if(ans<=)ans+=b;
- return ans;
- }
- int main(){
- LL T,a,b,d,x,y;
- scanf("%d",&T);
- while(T--){
- scanf("%lld%lld",&a,&b);
- LL ans=cal(a,b,);
- if(ans==-)puts("Not Exist");
- else printf("%lld\n",ans);
- }
- return ;
- }
题上数据比较水,数据范围1000,暴力一下就可以了:
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- using namespace std;
- const int INF=0x3f3f3f3f;
- typedef long long LL;
- int main(){
- int T,a,m;
- scanf("%d",&T);
- while(T--){//(1-ax)%m;
- scanf("%d%d",&a,&m);
- int flot=;
- for(int x=;x<=;x++){
- if((-a*x)%m==){
- flot=;
- printf("%d\n",x);
- break;
- }
- }
- if(flot)continue;
- puts("Not Exist");
- }
- return ;
- }
Modular Inverse(模逆元,扩展欧几里德)的更多相关文章
- 51Nod 1256 求乘法逆元--扩展欧几里德
#include<stdio.h> int exgcd(int a,int b,int &x,int &y) { ) { x=; y=; return a; } int r ...
- ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元
题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- Modular Inverse(zoj3609+欧几里德)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))
Invoker Time Limit : 2000/1000ms (Java/Other) Memory Limit : 122768/62768K (Java/Other) Total Subm ...
- 公钥密码之RSA密码算法扩展欧几里德求逆元!!
扩展欧几里得求逆元 实话说这个算法如果手推的话问题不大,无非就是辗转相除法的逆过程,还有一种就是利用扩展欧几里德算法,学信安数学基础的时候问题不大,但现在几乎都忘了,刷题的时候也是用kuangbin博 ...
- POJ-1061青蛙的约会,扩展欧几里德求逆元!
青蛙的约会 以前不止一次看过这个题,但都没有去补..好吧,现在慢慢来做. 友情提示 ...
- 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm
欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...
随机推荐
- 笔记-linux下Qt5.3.2 静态编译
这里主要讲linux下的编译,windows下面比较简单 参考:http://qt-project.org/wiki/Building-Qt-5-from-Git 依赖 sudo apt-get in ...
- Labview学习之远程控制VI
Labview学习之远程控制VI 从LabVIEW 6.1开始,LabVIEW集成了Remote Panels技术,允许用户直接在客户端计算机上打开并操作位于服务器端计算机上的VI的前面 ...
- windows下安装testlink
因为项目中一直没有使用任何测试用例管理工具,如果需要的时候都是个人写在的excle里各自保存,因为没有系统的记录当时测试方法和测试用例,每次需要再次测试已有的功能时,因为时间太长,而往往记不得当时是怎 ...
- 【转】杭电ACM试题分类
注:网上搜的 第一篇 1001 这个就不用说了吧1002 简单的大数1003 DP经典问题,最大连续子段和1004 简单题1005 找规律(循环点)1006 感觉有点BT的题,我到现在还没过1007 ...
- Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh
Hyperpolyglot Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh grammar | quoting and escaping | charactersvar ...
- one Infos
Backend Server is still running (PID:). Please try 'euc-server stop' first. (原因很简单,进程控制脚本无restart函数功 ...
- 手把手教你在openshift上搭建wordpress博客(二)
相同公布于:http://www.longgaming.com/archives/128 推荐前往阅读 这一篇文章主要介绍一些经常使用插件的使用和配置. 下面是我个人安装的一些插件.大家能够依据须要自 ...
- [学习笔记]HTML5之canvas
虐了一下午的canvas 先撸了一个七巧板 <!doctype html> <html> <head> <meta charset="utf-8&q ...
- 虎记:强大的nth-child(n)伪类选择器玩法
写在前面的戏: 最近参加了度娘前端IFE的春季班,刷任务,百度真是有营销头脑,让咱们这帮未来的技术狂人为他到处打广告(我可不去哪),其中做的几个任务中有几个以前没有用到的东西, 也算是有些许收获(现在 ...
- 经典mssql语句大全
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...