์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€(boj) 18512 ํŒŒ์ด์ฌ - ์ ํ”„ ์ ํ”„

  • -

๋ฌธ์ œ๋งํฌ : https://www.acmicpc.net/problem/18512

 

18512๋ฒˆ: ์ ํ”„ ์ ํ”„

์ฒซ์งธ ์ค„์— ๋‘ ์‚ฌ๋žŒ์ด ํ•œ ๋ฒˆ์— ๋ฉ€๋ฆฌ๋›ฐ๊ธฐ๋ฅผ ํ•˜๋Š” ๊ฑฐ๋ฆฌ X, Y์™€ ์‹œ์ž‘ ์ง€์ ์˜ ์œ„์น˜ ๊ฐ’ P1, P2๊ฐ€ ๊ฐ๊ฐ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ž์—ฐ์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ X, Y, P1, P2 ≤ 100)

www.acmicpc.net

 

 

์ฒ˜์Œ์—๋Š” ๊ทœ์น™์„ฑ์„ ์ฐพ์•„์„œ ์˜ˆ์™ธ์ผ๋•Œ -1์„ ์ถœ๋ ฅํ•˜๋„๋ก ์‹œ๋„ํ–ˆ๋Š”๋ฐ, ๊ทธ๋ƒฅ 1000ํšŒ ๊ณ„์‚ฐํ•ด๋ณด๊ณ  ๊ฐ™์€์ง€์ ์„ ์ง€๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด

-1์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ๋‹ค.

*ํŒŒ์ด์ฌ ์ฝ”๋“œ

x, y , p1, p2 = map(int,input().split())

x_path = [p1] ; y_path=[p2] ; answer = -1 ; cnt = 1


while (True):
    p1+=x
    p2+=y
    x_path.append(p1) ; y_path.append(p2)
    if p1 in y_path or p2 in x_path:
        answer= min(p1,p2)
        break
    elif cnt > 1000 :
        break
    cnt+=1
print(answer)

 

*๋‹ค๋ฅธ์‚ฌ๋žŒ ์ฝ”๋“œ

x,y,p1,p2 = map(int,input().split())
runx = set(range(p1, 10**6, x))
runy = set(range(p2, 10**6, y))

print(10**6)
R = runx&runy
print(min(R) if R else -1)

๋‹ค๋ฅธ์‚ฌ๋žŒ์€ ์–ด๋–ป๊ฒŒ ํ–ˆ๋‚˜ ๊ถ๊ธˆํ–ˆ๋Š”๋ฐ, ์ด๋ถ„๋„ 10*6ํšŒ๋งŒํผ ์—ฐ์‚ฐํ•œ set์—์„œ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋‚˜ ์ฐพ๊ณ , ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•˜๋Š”

ํ˜•์‹์ด์—ˆ๋‹ค. ๋” ์ฐพ์•„๋ณด๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ, ๋ฐฉ๊ธˆ ์˜ฌ๋ผ์˜จ ๋ฌธ์ œ๋ผ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ์—†์—ˆ๋‹ค. 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.