ZOJ 3609 求逆元
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>0,满足ax==1 (mod m)。
a和m都很小,不超过1000。所以可以暴力枚举x从1开始枚举,
因为是对m取模的,所枚举x从1到m即可。另外m有可能等于1,
所以直接判断a*x%m==1会错。改用(a*x-1)%m==0即可,
另外也可以不用乘法,从(a-1)开始每次加a就可以了,但这都无所谓。
还有,循环终止应该到m,而不是(m-1),本来到m的话ax==0 (mod m)肯定不对,
但还是因为有m=1的问题:0==1 (mod 1) 否则ax==1会无解错掉。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int gcd(int x,int y){
int z;
while(y){
z=y;
y=x%y;
x=z;
}
return x;
}
int main(){
int t,a,m,i,ans;
scanf("%d",&t);
while(t--){
scanf("%d%d",&a,&m);
ans=gcd(a,m);
if(ans>)
printf("Not Exist\n");
else{
i=;
while((a*i-)%m!=) //这一步判断很重要
i++;
printf("%d\n",i);
}
}
return ;
}
ZOJ 3609 求逆元的更多相关文章
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- hdu 1576 求逆元
题意:给出n=A mod 9973和B,求(A/B) mod 9973 昨天用扩展欧几里得做过这题,其实用逆元也可以做. 逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元. 求逆元方 ...
- HDU4869:Turn the pokers(快速幂求逆元+组合数)
题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k) ...
- ZOJ 3609 Modular Inverse
点我看题目 题意 : 这个题是求逆元的,怎么说呢,题目看着很别扭....就是给你a和m,让你求一个最小的x满足a-1≡x (mod m).或者ax≡1 (mod m).通俗点说呢,就是找一个最小的x, ...
- codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- 51NOD 1258 序列求和 V4 [任意模数fft 多项式求逆元 伯努利数]
1258 序列求和 V4 题意:求\(S_m(n) = \sum_{i=1}^n i^m \mod 10^9+7\),多组数据,\(T \le 500, n \le 10^{18}, k \le 50 ...
- 51nod 1118 机器人走方格 解题思路:动态规划 & 1119 机器人走方格 V2 解题思路:根据杨辉三角转化问题为组合数和求逆元问题
51nod 1118 机器人走方格: 思路:这是一道简单题,很容易就看出用动态规划扫一遍就可以得到结果, 时间复杂度O(m*n).运算量1000*1000 = 1000000,很明显不会超时. 递推式 ...
随机推荐
- CSS找到 (div+css请讲)
CSS 定位和浮动 CSS 定位和浮动提供了一些特性,使用这些属性,你可以建立栏布局,的重叠布局的一部分,并有一些.也可多年来完成通常需要使用的多个表格能力完成的任务. 定位的基本思路是非常easy. ...
- Html.ActionLink(转载)
@Html.ActionLink 代码: <h2>HtmlHelper</h2>@Html.ActionLink("默认","Index" ...
- E - Catch That Cow
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- FreeCodeCamp:Confirm the Ending
要求: 检查一个字符串(str)是否以指定的字符串(target)结尾. 如果是,返回true;如果不是,返回false. 结果: confirmEnding("Bastian", ...
- VMware: linux起步提示 memory for crashkernel(0*0 to 0*0)not within permissible
(virtualbox/VMware)linux起步提示memoryforcrashkernel(0*0 to 0*0)notwithinpermissible http://www.myexcep ...
- jQuery.form 中的 ajaxForm() 和 ajaxSubmit()
官方例子 http://malsup.com/jquery/form/#ajaxForm官方API http://malsup.com/jquery/form/#api中文API http: ...
- MVC3 Razor @RenderSection
Mvc3的Razor视图引擎还提供了@RenderSection @RenderSection在母版页中占个位,然后让使用此母版页的子页自己去呈现他们的Section. 在母版页_Layout.csh ...
- 将HDC保存为BMP文件
HDC在MSDN中的全称为:The handle of device context.通常,我们都是用来做相应的显示操作. 熟悉WIN32的朋友对于其应该不会陌生,经常采用GetDC,G ...
- Exploring the MapBox stack: MBTiles, TileJSON, UTFGrids and Wax
转自:http://blog.thematicmapping.org/2012/11/exploring-mapbox-stack-mbtiles-tilejson.html In my last b ...
- More Divisors(反素数)
More Divisors Time Limit: 2 Seconds Memory Limit: 65536 KB Everybody knows that we use decimal ...