ZOJ 3609 Modular Inverse(扩展欧几里德)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712
The modular modular multiplicative inverse of an integer a modulo
m is an integer x such that a-1≡x (mod. This is equivalent to
m)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
References
代码例如以下:
#include <cstdio>
#include <cstring>
#include <cmath>
typedef long long LL; LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
LL r = exgcd(b,a%b,x,y);
LL t = x;
x = y;
y = t-a/b*y;
return r;
}
} LL cal(LL a, LL b, LL c)
{
LL x, y;
LL tt = exgcd(a, b, x, y);
if(c%tt)//无整数解
{
return -1;
}
x*=c/tt;
b/=tt;
if(b<0)
b=-b;
LL ans=x%b;
if(ans<=0)
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, 1);
if(ans == -1)
{
printf("Not Exist\n");
continue;
}
printf("%lld\n",ans);
}
return 0;
}
ZOJ 3609 Modular Inverse(扩展欧几里德)的更多相关文章
- ZOJ 3609 Modular Inverse(拓展欧几里得求最小逆元)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- 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
点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...
- zjuoj 3609 Modular Inverse
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...
- 【ZOJ】3609 Modular Inverse
1. 题目描述求乘法逆元. 2. 基本思路利用扩展gcd求逆元,模板题目. 3. 代码 /* 3609 */ #include <iostream> #include <sstrea ...
- Modular Inverse(模逆元,扩展欧几里德)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- Modular Inverse(zoj3609+欧几里德)
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- (扩展欧几里德算法)zzuoj 10402: C.机器人
10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...
随机推荐
- [NOI2005]聪聪与可可
题目大意:有小a和小b,其中一个人到处乱走,每次走一步:另一个人抄近路逼近,每次1-2步.求期望路程. 整解:跑1000遍最短路/bfs,求两两距离,然后找从x逼近y第一步去哪,最后期望dp收场. d ...
- 使用 PHP + shell 生成 一键设置权限的脚本。
linux 系统 支持PHP脚本一键设置环境.shell脚本一键设置环境.那么 我今天 使用 PHP + shell 生成 一键设置权限的脚本. 举例子:linux服务器 一键配置discuz网站环 ...
- CSS九宫格样式
CSS .main>div { width: 14%; min-width: 160px; padding: 2%; height: 60px; border: 1px solid #f4f4f ...
- 第十八节:Scrapy爬虫框架之settings文件详解
# -*- coding: utf-8 -*- # Scrapy settings for maoyan project## For simplicity, this file contains on ...
- LeetCode(47)Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- Zoj 3781(构造)
Zoj 3781(构造) Zoj 3781 As we all know, Coach Gao is a talented chef, because he is able to cook M dis ...
- 第七章习题G题
题意 给出如图案例,要你从某一点开始走,一直走到极限(即无法再进行扩展),这时你走过的点会连成一个数,不同的走法当然会有不同的数,要求是输出最大的数(注意每个方块走过一次就不能再走) 思路 •1.枚举 ...
- POJ 3264 RMQ问题 用dp解决
#include <cstdio> #include <cstring> #include <iostream> using namespace std; ; #d ...
- java服务器图片压缩的几种方式及效率比较
以下是测试了三种图片压缩方式,通过测试发现使用jdk的ImageIO压缩时间更短,使用Google的thumbnailator更简单,但是thumbnailator在GitHub上的源码已经停止维护了 ...
- python:functools之partial
示例:from operator import addimport functoolsprint add(1,2) #3add1 = functools.partial(add,1)print add ...