Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 슬라이딩 윈도우
- 2357
- 투 포인터
- Python
- 13164
- SSAFY
- DFS
- 세그먼트 트리
- 구현
- 정렬
- DP
- 해시 테이블
- 수학
- 애드 혹
- 누적 합
- 문자열
- 그리디
- 맵
- 에라토스테네스의 체
- 싸피
- 모던 JavaScript 튜토리얼
- 브루트포스
- 정수론
- BFS
- 이분 탐색
- 트리
- JavaScript
- boj
- 그래프
- 플로이드-워셜
Archives
- Today
- Total
흙금이네 블로그
[BOJ] 14476 - 최대공약수 하나 빼기 (Python, JavaScript) 본문
아이디어
임의의 수 K를 중심으로 양쪽으로 누적된 두 최대공약수의 최대공약수의 최댓값을 구한다.
풀이 #1 (Python)
누적 합 알고리즘과 비슷한 원리로 합이 아닌 최대공약수를 누적해 리스트에 저장해 나간다.
import sys
input = sys.stdin.readline
def solution():
def gcd(a, b):
if a > b:
a, b = b, a
while a:
r = b%a
b, a = a, r
return b
N = int(input())
numbers = tuple(map(int, input().split()))
gcd_list = [0]*N
gcd_list[1] = numbers[0]
res = idx = -1
for i in range(1, N-1):
gcd_list[i+1] = gcd(gcd_list[i], numbers[i])
if gcd_list[i+1] == gcd(numbers[i], gcd_list[i+1]):
gcd_list[i+1] = -1
temp = numbers[-1]
if temp > res:
res = gcd_list[-1]
idx = N-1
for i in range(N-1, 0, -1):
temp = gcd(temp, numbers[i])
if gcd_list[i-1] == -1:
continue
gcd_list[i-1] = gcd(gcd_list[i-1], temp)
if gcd_list[i-1] == gcd(numbers[i-1], gcd_list[i-1]):
gcd_list[i-1] = -1
if gcd_list[i-1] > res:
res = gcd_list[i-1]
idx = i-1
if res >= 0:
print(res, numbers[idx])
else:
print(res)
solution()
풀이 #2 (JavaScript)
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split('\n');
function solution() {
function gcd(a, b) {
if (a > b) [a, b] = [b, a];
while (a > 0) {
r = b%a;
[b, a] = [a, r];
}
return b;
}
const N = Number(input[0]);
const numbers = input[1].split(' ').map(Number);
let gcdList = Array(N).fill(0);
gcdList[1] = numbers[0];
let res = idx = -1;
for (let i=1; i<N-1; i++) {
gcdList[i+1] = gcd(gcdList[i], numbers[i]);
if (gcdList[i+1] === gcd(numbers[i], gcdList[i+1])) gcdList[i+1] = -1;
}
temp = numbers[N-1];
if (temp > res) {
res = gcdList[N-1];
idx = N-1;
}
for (let i=N-1; i>0; i--) {
temp = gcd(temp, numbers[i]);
if (gcdList[i-1] === -1) continue;
gcdList[i-1] = gcd(gcdList[i-1], temp);
if (gcdList[i-1] === gcd(numbers[i-1], gcdList[i-1])) gcdList[i-1] = -1;
if (gcdList[i-1] > res) {
res = gcdList[i-1];
idx = i-1;
}
}
if (res >= 0) console.log(res, numbers[idx]);
else console.log(res);
}
solution();
'알고리즘' 카테고리의 다른 글
[BOJ] 1222 - 홍준 프로그래밍 대회 (Python, JavaScript) (0) | 2023.05.11 |
---|---|
[BOJ] 12764 - 싸지방에 간 준하 (Python, JavaScript) (1) | 2023.05.10 |
[BOJ] 11689 - GCD(n, k) = 1 (Python, JavaScript) (0) | 2023.05.09 |
[BOJ] 19582 - 200년간 폐관수련했더니 PS 최강자가 된 건에 대하여 (Python, JavaScript) (0) | 2023.05.09 |
[BOJ] 1415 - 사탕 (Python, JavaScript) (0) | 2023.05.08 |
Comments