B - Modular Inverse
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.
<h4< dd="">Output
For each test case, output the smallest positive x. If such x doesn't exist, output "Not Exist".
<h4< dd="">Sample Input
3
3 11
4 12
5 13
<h4< dd="">Sample Output
4
Not Exist
8 这题就是求乘法逆元
我用的方法比较复杂,我是这么想的,先判断a和f是不是互质,如果互质才有乘法逆元,否则没有乘法逆元,费马小定理可以求出膜是素数的乘法逆元,欧拉定理可以求出膜是非素数的乘法逆元:
具体方法:费马小定理,先要判断是不是素数,然后再用快速幂
欧拉定理,先要写欧拉函数,然后再用快速幂,其中欧拉函数需要一个质数的数组isp
所以用这种方法要写很多的函数,不过也好,昨天学的,正好好好的复习一下
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef long long ll;
const int maxn=1e5;
int p[maxn];//素数筛
void init()
{
for(int i=2;i<maxn;i++) p[i]=1;
for(int i=2;i*i<maxn;i++)
{
if(p[i])
{
for(int j=i*i;j<maxn;j+=i)
{
p[j]=0;
}
}
}
}
//v数组记录每一个i的最小质因数,isp记录所有的质数
int v[maxn],isp[maxn],m;
void init1()
{
for(int i=2;i<maxn;i++)
{
if(v[i]==0)
{
isp[m++]=i;
v[i]=i;
}
for(int j=0;j<m;j++)
{
if(v[i]<isp[j]||i*isp[j]>maxn) break;
v[i*isp[j]]=isp[j];
}
}
} int gcd(int a,int b)
{
return b==0? a:gcd(b,a%b);
}
int euler(int n)
{
int res=n;
for(int i=0;i<m;i++)
{
if(n%isp[i]==0)
{
res=res*(isp[i]-1)/isp[i];
}
}
return res;
}
int mod;
ll mod_pow(ll x,int n)
{
ll ans=1;
while(n)
{
if(n & 1) ans=ans*x%mod;
x=x*x%mod;
n>>=1;
}
return ans;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int f;
ll a;
scanf("%I64d%d",&a,&f);
init();
init1();
mod=f;
int ans;
if(gcd(a,f)==1)
{ if(p[f])//费马小定理
{
ans=mod_pow(a,f-2);
}
else//欧拉定理
{
int exa=euler(f);
ans=mod_pow(a,exa-1);
}
}
else {
printf("Not Exist\n");
continue;
}
printf("%d\n",ans);
}
return 0;
}
B - Modular Inverse的更多相关文章
- 寒假 D3 D Modular Inverse
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB ...
- zjuoj 3609 Modular Inverse
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609 Modular Inverse Time Limit: 2 Seco ...
- 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 ...
- 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 ...
- [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]
Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...
- 【ZOJ 3609】Modular Inverse 最小乘法逆元
The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x ...
- Modular Inverse (拓展欧几里得求逆元)
The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x ( ...
随机推荐
- 【Shell实战】批量在多台服务器上执行命令
功能说明:批量在多台服务器上执行命令 #!/bin/bash # ========================================== # 功能:批量在多台服务器上执行命令 # 方法: ...
- asp.net mvc 碰到 XML 解析错误:找不到根元素 位置
具体报错信息如下: XML 解析错误:找不到根元素 位置:moz-nullprincipal:{4a1d2b7c-6d07-468e-9df9-2267a0422c93} 行 1,列 1: 网上给出的 ...
- U盘基本处理,U盘与移动固态硬盘
一.辨别 USB2.0 和 USB3.0 1.从USB外观上来看,USB2.0通常是白色或黑色,而USB3.0则改观为“高大上”的蓝色接口. 目前,部分笔记本电脑USB接口,已同时提供对USB2.0及 ...
- Java容器类源码分析之Iterator与ListIterator迭代器(基于JDK8)
一.基本概念 迭代器是一个对象,也是一种设计模式,Java有两个用来实实现迭代器的接口,分别是Iterator接口和继承自Iterator的ListIterator接口.实现迭代器接口的类的对象有遍历 ...
- java_单词长度
题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束.你要输出这行文本中每个单词的长度.这里的单词与语言无关,可以包括各种符号,比如“it's”算一个单词,长度为4.注意,行中 ...
- Pseudocode MD5 CODE
//Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating var int[64] s, K //s ...
- Ajax实现的城市二级联动三
把之前2篇整合在一起 1.html <select id="province"> <option>请选择</option> </selec ...
- 解决VM提示:VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the program, access all directories the program uses, and access all directories for temporary files.
问题: 在开启虚拟机的时候报: VMware Workstation cannot connect to the virtual machine. Make sure you have rights ...
- Python十讲
第一讲:从零开始学Python 第二讲:变量和基础数据类型 第三讲:条件分支以及循环 第四讲:列表与元组 第五讲:字典 第六讲:函数 第七讲:类 第八讲:标准库 第九讲:异常 第十讲:文件处理
- 洛谷P2178 [NOI2015]品酒大会(后缀自动机 线段树)
题意 题目链接 Sol 说一个后缀自动机+线段树的无脑做法 首先建出SAM,然后对parent树进行dp,维护最大次大值,最小次小值 显然一个串能更新答案的区间是\([len_{fa_{x}} + 1 ...