확장 유클리드 호제법 예제 - 1
Last updated
Last updated
a b c 입력
gcd(a, b):
if b가 0:
a가 최대 공약수
else:
gcd(작은 수, 큰 수 % 작은 수)
# 확장 유클리드 호제법 구현
solution(a, b):
if b가 0:
재귀 함수를 중단하고 x, y를 각각 1, 0으로 설정하여 return
quot(a를 b로 나눈 몫)
solution(b, a % b) # 재귀 호출
x = y', y = x' - y' * 몫을 계산하는 로직 구현
# 재귀에서 빠져나오는 영역에서 실행하면 자연스럽게 역순이 된다.
계산된 x y return
최대 공약수 = gcd(a, b)
if c가 최대 공약수의 배수가 아니면:
-1 출력
else:
나머지(b)가 0이 될 때까지 확장 유클리드 호제법 함수 호출
결괏값에 c/최대 공약수의 값을 곱한 후 해당 값 출력a, b, c = map(int, input().split())
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def solution(a, b):
temp = [0] * 2
if b == 0:
temp[0] = 1
temp[1] = 0
return temp
quot = a // b
v = solution(b, a % b)
temp[0] = v[1]
temp[1] = v[0] - v[1] * quot
return temp
mgcd = gcd(a, b)
if c % mgcd != 0:
print(-1)
else:
quot = int(c / mgcd)
temp = solution(a, b)
print(temp[0] * quot, end=' ')
print(temp[1] * quot)import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int gcd = gcd(a, b);
if (c % gcd != 0) {
System.out.println(-1);
} else {
int quot = c / gcd;
int[] result = solution(a, b);
System.out.println(result[0] * quot + " " + result[1] * quot);
}
}
private static int[] solution(int a, int b) {
int[] temp = new int[2];
if (b == 0) { //나머지가 0일 때
temp[0] = 1; //x는 1
temp[1] = 0; //y는 0
return temp;
}
int quot = a / b;
int[] v = solution(b, a % b);
//역순으로 계산 실행
temp[0] = v[1]; // x = y'
temp[1] = v[0] - v[1] * quot; // y = x' - y' * 몫
return temp;
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
}