백준 15651번 N과 M (3) 문제

 

225932KB, 2648MS

 

이전번 문제와 동일하게 itertools의 product(중복순열)을 이용하였다.

특이하게 매개 값에 2 같은 정수형 자료가 들어가면 안되고

repeat = (int) x 로 반복 수를 넣어주어야 한다.

 

from itertools import product
n, m = map(int, input().split())
data = [i for i in range(1, n + 1)]
for i in product(data, repeat = m):
    for j in i:
        print(j, end=' ')
    print()

 

 

백준 9020 골드바흐의 추측 문제

 

시간 초과의 굴레.....

 

 

처음에는 주어진 수를 2개의 중복순열로 리스트를 만들어

해당 리스트가 전부 소수로 되어있으면 추출하는 방식으로 했는데, 그 수많은 수들을 매 케이스마다 반복하려니

시간초과가 나왔다.

 

from itertools import product
import math
for _ in range(int(input())):
    target = int(input())
    array = [True for i in range(target + 1)]
    result = []

    for i in range(2, int(math.sqrt(target)) + 1):
        if array[i]:
            j = 2
            while i * j <= target:
                array[i * j] = False
                j += 1

    data = []
    for i in range(2, target + 1):
        if array[i]:
            data.append(i)

    result = []
    for i in product(data, repeat=2):
        if i[0] + i[1] == target:
            result.append(i)

    if len(result) % 2 == 0:
        index = len(result) // 2 - 1
        print(result[index][0], result[index][1])
    else:
        index = len(result) // 2
        print(result[index][0], result[index][1])

최초 코드

 

 

그래서 결국 처음에 에라토스테네스의 체로 구별해 낸 소수 리스트을 1회 이용하여 연산해야 할 방법을 찾아야 했다.

중간값부터 1씩 올리고 / 1씩 내리고 를 반복하며 찾아가는 방식으로 n/2 회로 시간 복잡도를 줄여나갔다.

 

import math, sys
input = sys.stdin.readline
array = [True for i in range(10001)]

for i in range(2, 101):
    if array[i]:
        j = 2
        while i * j <= 10000:
            array[i * j] = False
            j += 1

for _ in range(int(input())):
    target = int(input())
    x = target // 2
    y = x

    for _ in range(10000):
        if array[x] and array[y]:
            print(x, y)
            break
        x -= 1
        y += 1

결과 코드

 

백준 4948번 베르트랑 공준 문제

 

에라토스테네스의 체를 이용했던 문제와 유사하다.

체를 이용해 주어진 수의 범위를 n ~ 2n으로 설정하여 소수가 나올때마다 count를 올려주기만 하면 된다.

 

import math
import sys
input = sys.stdin.readline

start = 1
while True:
    start = int(input())
    if start == 0:
        break
    end = 2 * start
    count = 0

    if start == 1:
        print("1")
        continue

    array = [True for i in range(end + 1)]

    for i in range(2, int(math.sqrt(end)) + 1):
        if array[i]:
            j = 2
            while i * j <= end:
                array[i * j] = False
                j += 1

    for i in range(start + 1, end + 1):
        if array[i]:

            count += 1

    print(count)

 

 

택시 기하학? 이라길래 위 사진의 택시를 생각했는데, 헤르만 민코프스키가 고안안 비유클리드 기하학의 택시 기하학을 의미하는 것이었다.  

 

단순 식으로 r이 주어졌을 때

유클리드 기하학에서는 r * r * 3.14159265358979323846....

택시 기하학에서는 r * (r * 2) 로 나타낼 수 있다.

 

코드로 옮겨주기만 하면 끝

 

r = int(input())
n1 = r * r * 3.14159265358979323846
n2 = r * (r * 2)
print("%.6f" %n1)
print("%.6f" %n2)

123316KB, 104MS로 해결

 

+ Recent posts