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( ...
随机推荐
- 最近比较迷flash professional cc 做PPT,做一个flash做动态打字效果的教程
想做一个flash打字效果.网上的方法要不是太繁琐,要不然就是各种遗漏.在这边做一个行之有效的flash做打字效果教程. 首先我用的是最新版本的flash professional cc .但是应该和 ...
- javaTemplates-学习笔记二
配置PlayFramework环境 下载jar包[Play with Activator],这一步有点晕是JAVA程序员CMD执行的步骤;运行了jar包下载了很多配置文件什么的,有的资源没有VPN链接 ...
- FZU Problem 1686 神龙的难题 重复覆盖
题目链接 给出大矩形的长宽, 矩形里面有1,0两个值, 给出小矩形的长宽, 求用最少的小矩形覆盖所有的1. 重复覆盖的模板题. #include <iostream> #include & ...
- SublimeLinter
SublimeLinter 关于代码检查 郑重推荐这个插件 官方插件库中仅有sublime3 有此插件 (对于ST2 有个sublimeLinter for ST2 没试过) SublimeLinte ...
- 服务器是R710常见错误汇总:
报错: E1422 CPU 1 machine check error . power cycle AC 解决方案: 系统 BIOS 已报告机器检查错误.请断开系统的交流电源 10 秒,然后重新启动系 ...
- JAVA并发,线程异常捕获
由于线程的特性,当我们启动了线程是没有办法用try catch捕获异常的,如下例: package com.xt.thinks21_2; import java.util.concurrent.Exe ...
- 激活工具 – Microsoft Toolkit 2.4.7
Microsoft Toolkit是一款很出名的Windows/Office激活工具,最早是因为激活Office 2010出名的,想必不少人也用过吧?Microsoft Toolkit从2.4.1版本 ...
- pyqt开发教程-搭建环境和开发示例
搭建环境和开发示例 * 安装 安装包 要对应python的版本 32位安装包(我PC上) http://jaist.dl.sourceforge.net/project/pyqt/PyQt4/PyQt ...
- HDU 5828 Rikka with Sequence(线段树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5828 [题目大意] 给出一个数列,要求支持区间加法,区间开方和区间和查询操作. [题解] 考虑开方 ...
- UVA 11475 Extend to Palindrome(后缀数组+ST表)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...