P1082 congruence equation (expand Euclid)

Title Description

Seeking on x with x in the congruence equation  AX \. 1 equiv \ {B} PMOD A x . 1 (MOD smallest positive integer b) solution.

Input and output formats

Input formats:

 

Line, comprising two positive integers  A, B A , B, separated by a space.

 

Output formats:

 

A positive integer  x_0 X 0 , i.e., the smallest positive integer solutions. Input data guaranteed solvable.

 

Sample input and output

Input Sample # 1:  Copy
3 10
Output Sample # 1:  Copy
7

Explanation

【data range】

Data for 40%, 2 ≤b≤ 1,000 2 B . 1 , 0 0 0;

For 60% of the data, 2 ≤b≤ 50,000,000 2 B . 5 0 , 0 0 0 , 0 0 0;

To 100% of the data, 2 ≤A, 2,000,000,000 b ≤ 2 A , B 2 , 0 0 0 , 0 0 0 , 0 0 0.

NOIP 2012 to improve the group the next day the first question

Code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    ll ans=exgcd(b,a%b,x,y);
    ll temp=x;
    x=y;
    y=temp-(a/b)*y;
    return ans;
}
int main()
{
    ll a,b;
    ll x=0,y=0;
    cin>>a>>b;
    ll ans=exgcd(a,b,x,y);
    cout<<(x%b+b)%b<<endl;
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Staceyacm/p/11230238.html
Recommended