[BOJ][๐ŸŸก4][๋ฐฑ์ค€#26092] ์ˆ˜ํ•™์ ์ธ ์ตœ์†Œ ๊ณตํ†ต ์กฐ์ƒ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #26092


๋ฌธ์ œ

$10^{12}$ ๊ฐœ์˜ ์ •์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํŠธ๋ฆฌ์˜ ๊ฐ ์ •์ ์€ $1$๋ฒˆ๋ถ€ํ„ฐ $10^{12}$๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๊ณ  $1$๋ฒˆ ์ •์ ์€ ๋ฃจํŠธ์ด๋‹ค. ์ด ํŠธ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ˆ˜ํ•œ ์„ฑ์งˆ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

$2$ ์ด์ƒ $N$ ์ดํ•˜์˜ ์ •์ˆ˜ $x$์— ๋Œ€ํ•ด $x$์˜ ๊ฐ€์žฅ ์ž‘์€ ์†Œ์ธ์ˆ˜๋ฅผ $p$๋ผ๊ณ  ํ•˜์ž. $x$๋ฒˆ ์ •์ ์˜ ๋ถ€๋ชจ ์ •์ ์€ $\frac{x}{p}$๋ฒˆ ์ •์ ์ด๋‹ค.

๋‘ ์ •์ ์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต ์กฐ์ƒ์€, ๋‘ ์ •์ ์„ ๋ชจ๋‘ ์ž์†์œผ๋กœ ๊ฐ€์ง€๋ฉด์„œ ๊นŠ์ด๊ฐ€ ๊ฐ€์žฅ ๊นŠ์€ ์ •์ ์œผ๋กœ ์ •์˜ํ•œ๋‹ค. ๋‘ ์ •์ˆ˜ $a$์™€ $b$๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, $a$๋ฒˆ ์ •์ ๊ณผ $b$๋ฒˆ ์ •์ ์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต ์กฐ์ƒ์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ตฌํ•ด๋ณด์ž.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ $a$์™€ $b$๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. $(1\leq a,b\leq 10^{12})$


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— $a$๋ฒˆ ์ •์ ๊ณผ $b$๋ฒˆ ์ •์ ์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต ์กฐ์ƒ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

1 4


์ถœ๋ ฅ

1


์˜ˆ์ œ 2

์ž…๋ ฅ

21 49


์ถœ๋ ฅ

7


์˜ˆ์ œ 3

์ž…๋ ฅ

30 20


์ถœ๋ ฅ

5


My Sol

def check_parent(N):
    S = set([1, N])
    while N > 1:
        flag = 0
        for n in range(2, int(N**(1/2))+1):
            if N%n: continue
            N = N//n
            S.add(N)
            flag = 1
            break
        if not flag: break
    return S

A, B = map(int, input().split())
A_Set = check_parent(A)
B_Set = check_parent(B)
U_Set = A_Set & B_Set
print(max(list(U_Set)))

์ž…๋ ฅ์ด 10์˜ 12์Šน์ด๋ฏ€๋กœ, ์†Œ์ˆ˜ํŒ์ •์—์„œ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ฒ˜๋Ÿผ ์ œ๊ณฑ๊ทผ๊นŒ์ง€ ๋‚˜๋ˆ ๋ณด๋Š” ๋ฐฉ์‹์„ ํ™œ์šฉํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ•œ ๋ฐ˜๋ณต๋‹น 10์˜ 6์Šน, ์ฆ‰ 100๋งŒ์œผ๋กœ ์—ฐ์‚ฐ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ตœ๋Œ€ 100๋งŒ์˜ ์ˆ˜๊นŒ์ง€ N์„ ๋‚˜๋ˆ ๋ณด๋ฉฐ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.


  1. ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•ด ๋‘ ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค.
  2. ์ˆ˜๋ฅผ ๊ฐ€์žฅ ์ž‘์€ ์†Œ์ธ์ˆ˜๋กœ ๋‚˜๋ˆ„๋ฉฐ ์ง‘ํ•ฉ์— ๋„ฃ๋Š”๋‹ค. ๋ถ€๋ชจ ์ •์ ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋ถ€๋ชจ ์ •์ ์˜ ๋ถ€๋ชจ ์ •์ ์„ ๋‹ค์‹œ ๊ตฌํ•ด๊ฐ€๋Š” ๋ฐ˜๋ณต์„ ์‹ค์‹œํ•œ๋‹ค.
  3. ์ด ๋ฐ˜๋ณต์€ ์†Œ์ธ์ˆ˜๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†๋Š” ์†Œ์ˆ˜๊ฐ€ ๋˜๊ฑฐ๋‚˜ 1์ด ๋˜๋ฉด ์ข…๋ฃŒ๋œ๋‹ค. ์ด๋•Œ๊นŒ์ง€ ์ง‘ํ•ฉ์„ ๊ตฌํ•œ ๋’ค ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  4. ๋‘ ์ˆ˜์— ๋Œ€ํ•œ ๋ถ€๋ชจ ์ •์  ์ง‘ํ•ฉ์„ ๋ชจ๋‘ ๊ตฌํ•œ ๋’ค, ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•œ๋‹ค. ์ตœ์ƒ์œ„ ์ •์ ์ธ 1๋ฒˆ ์ •์ ์„ ๊ณต์œ ํ•˜๋ฏ€๋กœ ๋ชปํ•ด๋„ 1๋ฒˆ ์ •์ ์„ ๊ฐ€์ง„ ๊ต์ง‘ํ•ฉ์ด ์ƒ๊ธด๋‹ค. ์ด ๊ต์ง‘ํ•ฉ์€ ๊ณต์œ ํ•˜๋Š” ๋ถ€๋ชจ ์ •์ ์ด๋‹ค.
  5. ์ด ๊ต์ง‘ํ•ฉ ์›์†Œ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ