您的位置:首页 > 其它

ZOJ 3609 Modular Inverse【同余问题】

2016-04-21 18:15 381 查看
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

References

http://en.wikipedia.org/wiki/Modular_inverse
Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

题目大意:求符合公式的a*x≡1(mod m)的最小x。

这其实就是个对概念理解的题目,表示数论渣简直哭到死。

首先对于这个公式的认识,我们发现,其中的等号是三个横,而且他的意义也不是恒等,其含义是同余。

对于同余的概念,我们的度娘是这样告诉我们滴:数论中的重要概念。给定一个正整数m,如果两个整数a和b满足(a-b)能够整除m,即(a-b)/m得到一个整数,那么就称整数a与b对模m同余,记作a≡b(mod
m)。对模m同余是整数的一个等价关系

辣么为什么要引出同余这个概念呢,大哥我看样例就懂了这个题是什么意识好吗?但是你去敲吧,m==1的时候不输出1你就敲吧,敲死你你也过不掉。

a*1≡1(mod 1)按照定理所述,这个式子表示a%1==1%1,所以要输出1.

AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int a,mod;
scanf("%d%d",&a,&mod);
if(mod==1)
{
printf("1\n");
continue;
}
int i;
for(i=0;i<1000000;i++)
{
//printf("%d\n",a*i%mod);
if((a*i)%mod==1)
{
printf("%d\n",i);
break;
}
}
if(i==1000000)
printf("Not Exist\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ZOJ 3603