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 ...
随机推荐
- MySQL pt-table-checksum及pt-table-sync校验及修复主从一致性
[pt-table-checksum]pt-table-checksum是percona-toolkit系列工具中的一个, 可以用来检测主. 从数据库中数据的一致性.其原理是在主库上运行, 对同步的表 ...
- 修复 海盗船 k70 lux 未检测到设备(k70 no device detected)
corsair k70 lux 上周收到的生日礼物,头一次用机械键盘,还是这么高端的机械键盘(729RMB),手感一级棒.但是,有问题啊!把键盘上的 bios按钮拨到8上电脑可以识别,scroll 灯 ...
- WiFi无线网络参数 802.11a/b/g/n 详解
转载自:WiFi无线网络参数 802.11a/b/g/n 详解 如转载侵犯您的版权,请联系:2378264731@qq.com 802.11a/b/g/n,其实指的是无线网络协议,细分为802.11a ...
- Linux系统中DHCP的配置
DHCP为动态主机配置协议,负责IP地址的动态分配(当一个主机的IP为自动,则不需要自己写IP,他会在DHCP服务器的范围内自动获取) 在真机或虚拟机下配置DHCP服务时,需要先下载dhcp软件,使用 ...
- 判断Git是否有新的提交
公司要搭建CI,有这样一个需求:判断Git是否有新的提交,如果有的话拉取代码构建,如果没有不构建,Jenkins的搭建这里就不赘述了,主要讲一下判断这里. Jenkins需要安装插件Condition ...
- linux:系统启动流程
系统启动流程 本文基于CentOS6 版本 黑色部分为主流程分支,蓝色部分为详细流程分支,绿色部分是注释部分 第一步--加载BIOS打开计算机电源,计算机会首先加载BIOS信息,主要负责检测系统外围关 ...
- (转) MapReduce Design Patterns(chapter 5 (part 1))(九)
Chapter 5. Join Patterns 把数据保存成一个巨大的数据集不是很常见.例如,用户信息数据频繁更新,所以要保存到关系数据库中.于此同时,web日志以恒定的数据流量增加,直接写到HDF ...
- egg.js源码解析之render()
作为阿里的开源node.js框架,我觉得egg.js是很有前途的,故而学之,github上down了一个项目下来(https://github.com/easy-team/egg-vue-webpac ...
- Http权威指南(cookie以及web认证机制)
其实对于cookie,想必大家都不陌生,cookie目前主要用于客户端的识别技术. 说到客户端识别技术,就不得不说一个登录态的问题了.登录态顾名思义,用于验证用户的登录与否. 1.登录态 对于PC端网 ...
- tensorflow_gpu-1.2.1-cp35-cp35m-win_amd64.whl文件怎么安装?
当你从迅雷下载之后,怎么样来安装上面的文件呢? 这也是一个问题,对于新手来说,是不知道的.其实比较简单,如下: 用命令:pip install tensorflow_gpu-1.2.1-cp35-c ...