Article Directory
Subject to the effect
Problem-solving ideas
Meaning of the questions we can ask whether there is an integer solution , such that . By theTheoremshows that if , it is clear that no solution. in case then use the extended Euclidean algorithm derived . 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
- Solution Conditions