ZOJ 3609 求最小逆元模板题
来源:互联网 发布:甲骨文软件 薪资 编辑:程序博客网 时间:2024/06/13 13:45
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
33 114 125 13
Sample Output
4Not Exist8
分析:ax=1(mod m); 我们称x是a关于m的最小乘法逆元。 相当于 a*x%m=1%m
那么(a*x-1)就必须是m的整数倍才行。所以我们设是m的y倍。
于是式子转化成: (a*x-1)=m*y;
那么 ax-my=1 要有解,相当于是 ax+my=1要有解,这里m如果是负的话,那么就写成正的好了,因为系数是没有关系的。
#include<iostream>#include<cstdio>using namespace std;typedef long long LL;void gcd(int a,int b,int &d,int &x,int &y){if(!b) {d=a,y=0,x=1;}else{gcd(b,a%b,d,y,x);y-=x*(a/b);}}int main(){//freopen("E:\\ACM\\test.txt","r",stdin);int T,a,m;cin>>T;while(T--){cin>>a>>m;int x,y,d,b=m,c=1;gcd(a,b,d,x,y);if(c%d) puts("Not Exist");else{//x=x*c/d;//b=b/d;x=x%b;if(x<=0) x+=b;//x=(x%b+b)%b;cout<<x<<endl;}}return 0;}
阅读全文
0 0
- ZOJ 3609 求最小逆元模板题
- zoj 3609(求最小逆元)
- ZOJ 3609 Modular Inverse(求最小乘法逆元)
- zoj 3609 Modular Inverse 求最小逆元 裸的的扩展欧几里得
- ZOJ 3609 Modular Inverse(扩展欧几里德求乘法逆元)
- Modular Inverse 【欧几里得求 最小逆元】
- 求最小生成元
- ZOJ 1729 & ZOJ 2006(最小表示法模板题)
- zoj 2676 dinic模板求实型最小割(可做dinic模板)
- ZOJ(Modular Inverse)——最小乘法逆元
- zoj 3792 求最小割边集
- ZOJ 1729 Hidden Password(最小表示法模板题)
- zoj 1090 求三角形的外心(模板题)
- Lightoj 1067【逆元模板(求C(N,M))】
- O(N) 求 1~N 逆元 模板及证明
- 【逆元】ZOJ 3609 Modular Inverse
- 【数学 逆元】zoj 3609 Modular Inverse
- ZOJ 3609 Modular Inverse(乘法逆元)
- 细说一个汉字等于几个字符,以及汉字,字符,字节,位之间的关系
- _T() 和_L() _TEXT __T,L区别与联系详解
- Android初级开发(四)——补充6、控件TabHost的使用
- VS2015无法使用编辑并继续 及 警告LNK4075的解决方法
- PHP 高级过滤器
- ZOJ 3609 求最小逆元模板题
- eclipse 与 android studio 一些常用的对应快捷键
- LeetCode 647. Palindromic Substrings--回文子串个数
- android 沉浸式状态栏 github地址
- Linux shell脚本中shift的用法说明
- 弱网下移动端网络连接处理策略
- 解决DBlink无法解析指定的连接标识符的问题
- 2016至2017年上半年iOS项目细节(1)
- python实现最大熵模型