ZOJ 4712: Modular Inverse

Modular Inverse

///@author Sycamore, ZJNU;
///@date 8/6/2017
///@ref stanford-acm-master
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int mod(int a, int b)
{
	return ((a%b) + b) % b;
}

int extended_euclid(int a, int b, int &x, int &y) {
	int xx = y = 0;
	int yy = x = 1;
	while (b) {
		int q = a / b;
		int t = b; b = a%b; a = t;
		t = xx; xx = x - q*xx; x = t;
		t = yy; yy = y - q*yy; y = t;
	}
	return a;
}
int mod_inverse(int a, int n) {
	int x, y;
	int g = extended_euclid(a, n, x, y);
	if (g > 1) return -1;
	return mod(x,n)==0?n:mod(x,n);
}

int main()
{
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	while (T--)
	{
		int a, m;
		cin >> a >> m;
		int mi = mod_inverse(a, m);
		if (~mi)cout << mi;
		else cout << "Not Exist";
		cout << "\n";
	}
	return 0;
} 
时间: 2024-08-04 14:25:20

ZOJ 4712: Modular Inverse的相关文章

ZOJ 3609 Modular Inverse (水题)

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. Th

ZOJ 3609 Modular Inverse 解线性模方程

点击打开链接 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 ca

ZOJ 3609 Modular Inverse

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=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).

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 m). This is equivalent to ax≡1 (mod m). Input There are multiple test cases.

ZOJ - 3609 —— Modular Inverse 【乘法逆,扩展欧几里得】

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 1. 这题数据范围太小,直接暴力即可 2. 不过其实这题也是很“直白的”求乘法逆的题目,即当b=1的特殊的模线性方程问题ax≡b mod(n),可以通过扩展欧几里得算法求解:   ax≡b mod(n) => (ax) mod n = b mod n => ax=k1*n+r ... (1) b=k2n+r    ... (2)  (1)-(2)=>ax-

Modular Inverse(zoj3609+欧几里德)

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. Th

Modular Inverse(模逆元,扩展欧几里德)

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. Th

寒假 D3 D Modular Inverse

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 Th

浙江2012年省赛J题 Modular Inverse

Modular Inverse Time Limit: 2000MS   Memory Limit: 65535KB   64bit IO Format: Submit Status Description 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). I