ZOJ 3609 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
题解:
最小乘法逆元:由ax≡1 (mod m)得:转化为解线性方程ax+by=1
需要注意的地方:最小解取模时不能写成(x%t+t)%t 因为此题要的是正数解 这样写有时会输出0
首先我来回顾下欧几里德的几个定理,有助于理解这道题;
定理一:如果d = gcd(a, b),则必能找到正的或负的整数k和l,使 d = a*x+ b*y。
定理二:若gcd(a, b) = 1,则方程ax ≡ c (mod b)在[0, b-1]上有唯一解。
定理三:若gcd(a, b) = d,则方程ax ≡ c (mod b)在[0, b/d - 1]上有唯一解。
对于ax+by=1; 即ax=1(mod b) 当且仅当gcd(a,b)!=1 的时候,无解!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm> #define INF 0x7fffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 100000 using namespace std; typedef long long LL;
typedef double DB; LL e_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x=;
y=;
return a;
}
LL ans=e_gcd(b,a%b,x,y);
LL temp=x;
x=y;
y=temp-a/b*y;
return ans;
} LL cal(LL a,LL b,LL c)
{
LL x,y;
LL gcd=e_gcd(a,b,x,y);
if(c%gcd!=) return -;
x*=c/gcd;
b/=gcd;
if(b<) b=-b;
LL ans=x%b;
if(ans<=) ans+=b;
return ans;
} int main()
{
LL a,b,t;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&a,&b);
LL ans=cal(a,b,);
if(ans==-) printf("Not Exist\n");
else printf("%lld\n",ans);
}
return ;
}
ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)的更多相关文章
- ZOJ 3593 One Person Game(拓展欧几里得求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)
gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...
- Modular Inverse (拓展欧几里得求逆元)
The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x ( ...
- ZOJ Problem Set - 3593 拓展欧几里得 数学
ZOJ Problem Set - 3593 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 One Person ...
- ZOJ——3609 Modular Inverse
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- ZOJ 3609 Modular Inverse(扩展欧几里得)题解
题意:求乘法逆元最小正正数解 思路:a*x≡1(mod m),则称x 是 a 关于 m 的乘法逆元,可以通过解a*x + m*y = 1解得x.那么通过EXGcd得到特解x1,最小正解x1 = x1 ...
- ZOJ 3609 Modular Inverse(扩展欧几里德)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...
- ZOJ 3609 Modular Inverse
点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...
- POJ 1061 青蛙的约会(拓展欧几里得求同余方程,解ax+by=c)
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 122871 Accepted: 26147 Descript ...
随机推荐
- System.Zip
自XE2增加的System.Zip单元很好.注意事项: 1.文件压缩到文档后所使用的文件名会成为解压后的文件名,如果该文件名为指定文件名且无后缀名,那么解压出来的文件名也没有后缀名:
- Alpha冲刺一(1/10)
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9948330.html 作业博客:https://edu.cnblogs.com/campus/ ...
- ubuntu安装搜狗输入法的相关问题
最近换了一次电脑,要重新装系统,就选择了ubuntu14.04 LTS版本的,这个版本官方支持到2019年了,所以相对来说比较稳定,也不用担心软件源的问题.不像14.10那样,现在官方已经没有软件源的 ...
- zookeeper的c API 单线程与多线程问题 cli_st和cli_mt
同样的程序,在centos和ubuntu上都没有问题,在solaris上问题却多多,据说是solaris管理更加严格. zookeeper_init方法,在传入一个错误的host也能初始化出一个非空的 ...
- mac下iterm2快捷方式
mac下iterm2,一些技巧,做个记录,大部分参考别人的加上自己的补充: 其中option + 左右键来跳转单词还是有问题,结果变为[D[C,等我摸索好了再来补充. 窗口 新建tab:⌘ + t 切 ...
- ionic安装插件常用命令
常见插件查找网站: http://ngcordova.com/docs/plugins http://cordova.apache.org/plugins/ $ ionic plugin list / ...
- windows下类似Linux下的grep命令
今天要查看windws下代理服务器有哪些IP连接过来,但使用 netstat -na 后出现很多连接会话,不方便查看. 想到Linux下的grep非常方便,于是网络上搜寻,还是有类似的命令findst ...
- Golang的简明安装指南
引言: Go language现在是大名鼎鼎,很多的开源项目都是基于go来做的,比如codis, ethereum等都用到了go lang,所以免不了要使用这个东东,本文将简明介绍安装步骤以及环境变量 ...
- angular 与jQuery混用 大坑一
由于angular是在真实dom加载之后,运行,所以jQuery操作dom时,要先于angular添加的元素(如,ng-repeat),所以常常会出现,元素错位,解决办法:添加一个透明的站位元素:
- 音乐随想——德沃夏克《From The New World》
第一乐章 前奏拖的很长,低音,再低音.突然转向,好像漂泊数月的水手看到了新大陆. 第二乐章 前奏很优美,到双簧管出现的时候宛若紫霞仙子撑船自芦苇荡中飘过. 之后又一段较前奏稍快的旋律,好像看到了梦寐的 ...