Challenges Programming Contest: Double Six

Subject to the effect

Here Insert Picture Description

Problem-solving ideas

Meaning of the questions we can ask whether there is an integer solution ( x , Y ) (X, y) , such that a x + b Y = 1 ax+by = 1 . By theTheoremshows that if g c d ( a , b ) ! = 1 gcd(a, b) != 1 , it is clear that no solution. in case g c d ( a , b ) = 1 gcd(a, b) = 1 then use the extended Euclidean algorithm derived ( x , y ) (X, y) . Specific Extended Euclidean Algorithm seehere

Code

#include<iostream>
using namespace std;

int extend_ojld(int a, int b, int &x, int &y)
{
    int d;
    if(b == 0)
    {
        x = 1;
        y = 0;
        d = a;
    }
    else
    {
        d = extend_ojld(b, a%b, y, x);
        y -= (a/b)*x;
    }
    return d;
}

int main()
{
    int a, b;
    cin >> a >> b;
    int x, y;
    int d = extend_ojld(a, b, x, y);
    int num[4] = {0, 0, 0, 0};
    if(x > 0)
        num[0] = x;
    else
        num[1] = -x;
    if(y > 0)
        num[2] = y;
    else
        num[3] = -y;
    if(1 % d)
        cout << -1 << endl;
    for(int i=0; i<4; i++)
        cout << num[i];
    cout << endl;
    return 0;
}

Knowledge Point

  • Extended Euclidean Algorithm
  • a x + b y = n ax + by = n Solution Conditions

Guess you like

Origin blog.csdn.net/Wangpeiyi9979/article/details/93722411