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-b=(k1-k2)n

记y=(k2-k1)=>ax+ny=b

转化为贝祖等式,这一经典的扩展欧几里得算法了。

具体解法见我之前的博客,传送门:http://www.cnblogs.com/AcIsFun/p/5393550.html

#include <cstdio>
#include <iostream>

using namespace std;

int exgcd(int a, int b, int &x, int &y)
{
    if(b==0) {
        x = 1;
        y = 0;
        return a;
    }
    int r = exgcd(b, a%b, x, y);
    int t = x;
    x = y;
    y = t - a/b*y;
    return r;
}

bool f(int a, int b, int c, int &x, int &y)
{
    int d = exgcd(a, b, x, y);
    if(c%d)    return 0;
    int k = c/d;
    x *= k;
    y *= k;
    return 1;
}

int main ()
{
    int T, a, n, x, y;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &a, &n);
        if(f(a, n, 1, x, y)) {
            if(x>0) {
                x = x%n;
            } else if(x < 0) {                // 这里基于 x+k*n>0 => k=(-x)/n+1
                x = (x+((-x)%n+1)*n)%n;
            }
            else    x++;
            printf("%d\n", x);
        } else {
            printf("Not Exist\n");
        }
    }
    return 0;
}
时间: 2024-08-12 07:08:35

ZOJ - 3609 —— 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.

hiho1530(乘法逆元)(扩展欧几里得)

#1530 : 分数取模 时间限制:1000ms 单点时限:10000ms 内存限制:256MB 描述 给定三个正整数 a. b 和 p,满足 b 和 p 互质.这时分数 a / b 除以 p 的余数,即 a / b MOD p 可以定义为 a × b-1 MOD p. 其中b-1 是 b 的逆元,它满足 1 ≤ b-1 < p 且 b × b-1 ≡ 1 MOD p,满足上述条件的 b-1有且仅有一个. 例如 2-1 ≡ 4 MOD 7,因为2 × 4 ≡ 1 MOD 7: 3-1 ≡ 3 M

51Nod 1256 乘法逆元(扩展欧几里得)

1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 typedef long long LL; 6 7 //给予二整数 a 与 b, 必存在有整数 x 与 y 使得ax + by = gcd(a,b) 8 LL extgcd(LL a, LL b, LL &x, LL &y){ 9 LL d = a; 10 if (b != 0){ 11 d = extgcd(b, a%b,

乘法逆元(扩展欧几里得)

#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #define ll long long using namespace std; ll n,p; void exgcd(ll a,ll b,ll &x,ll &y) { if(!b) { x=1;y=0;

【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】

Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到Catalan数,但是我却花了两个小时去找递推式. 首先 Catalan数 : 基本规律:1,2,5,14,42,132,.......... 典型例题: 1.多边形分割.一个多边形分为若干个三角形有多少种分法. C(n)=∑(i=2...n-1)C(i)*C(n-i+1) 2.排队问题:转化为n个人

自己动手写Java大整数《4》扩展欧几里得和Mod逆

/* *我把这个大整数的系列写成了Code中的项目,见https://code.csdn.net/XUE_HAIyang/bignumber */ 之前已经完成了大整数的表示.绝对值的比较大小.取负值.加减法运算.乘法运算以及除法和余数运算.具体见我的主页前三篇博客(自己动手写Java系列 ). 这篇博客添加求大整数GCD.扩展欧几里得算法和求Mod逆的算法. 扩展欧几里得算法 说道扩展的欧几里得算法,首先我们看下简单的欧几里得算法.经典的欧几里得算法就是 计算两个整数的最大公因子的算法,所基于