P1082 solution to a problem congruence equation

This question Source: NOIP2012Day2T1

The meaning of problems: of Equation \ (ax \ equiv1 \ pmod { b} \) the smallest positive integer solution \ (X \) .

\ [AX \ equiv1 \ PMOD {B} \]
\ [\ Rightarrow = AX +. 1 by \]
input data to ensure a solution, i.e. to ensure that the \ (\ gcd (a, b ) = 1 \) i.e. \ (A , b \) prime.
\ [\ Rightarrow ax + by =
gcd (a, b) \] directly apply the extended Euclidean \ ((exgcd) \) template can be solved. Note that the modulus \ (X \) is the smallest positive integer.

\(code:\)

#include<bits/stdc++.h>
using namespace std;
#define re register
#define ll long long
#define il inline
#define dou double
#define un unsigned
il int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
#define INF 114514114
#define clr(x) memset(x,0,sizeof(x))
int a,b,x,y;
il void exgcd(int a,int b,int &x,int &y)
{
    if(!b)
    {
        x=1;y=0;return ;
    }
    exgcd(b,a%b,y,x);
    y-=a/b*x;
}
int main()
{
    a=read();b=read();
    exgcd(a,b,x,y);
    cout<<(x%b+b)%b<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/Hakurei-Reimu/p/11518807.html
Recommended