목차

보석 가게

ps
링크acmicpc.net/…
출처BOJ
문제 번호13575
문제명보석 가게
레벨플래티넘 1
분류

고속 푸리에 변환

시간복잡도O(knlog(kn))
인풋사이즈n<=1000, k<=1000
사용한 언어Python
제출기록327724KB / 14020ms
최고기록14020ms
해결날짜2021/02/20

풀이

코드

"""Solution code for "BOJ 13575. 보석 가게".

- Problem link: https://www.acmicpc.net/problem/13575
- Solution link: http://www.teferi.net/ps/problems/boj/13575
"""

from teflib import fft


def main():
    N, K = [int(x) for x in input().split()]  # pylint: disable=unused-variable
    a = [int(x) for x in input().split()]
    jewel = [0] * (max(a) + 1)
    for a_i in a:
        jewel[a_i] = 1

    # Computes answer = jewel ^ K.
    answer = [1]
    for c in reversed(bin(K)):
        if c == '1':
            answer = [0 if x == 0 else 1 for x in fft.multiply(answer, jewel)]
        jewel = [0 if x == 0 else 1 for x in fft.multiply(jewel, jewel)]

    print(' '.join(str(i) for i, x in enumerate(answer) if x == 1))


if __name__ == '__main__':
    main()