나머지 숫자의 xor가 0인 부분 집합의 수를 찾습니다. (Find number of subsets, that xor of remaining numbers equals to 0)


문제 설명

나머지 숫자의 xor가 0인 부분 집합의 수를 찾습니다. (Find number of subsets, that xor of remaining numbers equals to 0)

n개의 숫자가 주어졌을 때, 나머지 숫자 중 0과 같은 부분집합의 최소 수를 찾으십시오. 예:

{1,1,3,4,5}

결과는 3과 같습니다. 왜냐하면 하위 집합 {1,3}(두 가지 방법으로) 또는 {3,4,5}을 삭제할 수 있기 때문입니다.

O( 2^n) 무차별 대입.


참조 솔루션

방법 1:

Let us consider a Dynamic Programming table of size n*m where m=10^4. We have an array of size n, such that 1 <= a[i] <= m.

Now, D[i][j] = number of subsets such that xor of the set is j Here xor of the set means xor of all elements of set.

D[i][j] = D[i‑1][j] + D[i‑1][j xor a[i]]. This recursive relation can be derived from that fact that new element a[i] will be present in subset or not.

If a[i] is not present => any subset of i‑1 elements whose xor is j

If a[i] is present => any subset of i‑1 elements whose xor is j xor a[i].( Because j xor a[i] xor a[i] = j)

In this way you are able to find all the subset whose xor is any given number. Note that this given number can be only as large as m. Coming to your question, it just boils down to finding out subset of elements whose xor is X, where X is xor of all elements.

Time : O(nm)

(by Steve MaddenRishit Sanmukhani)

참조 문서

  1. Find number of subsets, that xor of remaining numbers equals to 0 (CC BY‑SA 2.5/3.0/4.0)

#dynamic-programming #algorithm






관련 질문

나머지 숫자의 xor가 0인 부분 집합의 수를 찾습니다. (Find number of subsets, that xor of remaining numbers equals to 0)

재귀 대신 동적 프로그래밍을 사용하여 가장 큰 공통 접미사(자바스크립트) 찾기 (Use dynamic programming instead of recursion to find largest common suffix (javascript))

제거 방법 : java.lang.OutOfMemoryError (how to remove : java.lang.OutOfMemoryError)

가장 효율적인 좌석 배치 (Most efficient seating arrangement)

aglorithm의 복잡성을 가진 춤 (A dance with an aglorithm's complexity)

장애물이 있는 처음부터 끝까지 경로의 수를 계산합니다. (Count the number of paths from start to end with obstacles)

직사각형 필드 내에서 다양한 크기의 직사각형을 효율적으로 배치 (Efficient placement of variable size rectangles within a rectangular field)

기차 또는 버스를 이용할 수 있는 도시 간 최단 경로 DYNAMIC PROGRAMMING (Shortest path between cities that you can use either train or bus DYNAMIC PROGRAMMING)

인덱스가 오름차순으로 정렬되도록 주어진 합계를 갖는 하위 배열의 개수 (Count of sub-arrays with a given sum such that the indices are in ascending order)

2차원 배열의 경로 수 계산(그리드 Traveller) (Count number of paths on 2d-array (grid Traveller))

n개의 볼과 m개의 빈이 주어지고 각 빈이 특정 용량을 가질 때, 몇 개의 조합이 있습니까? (Given n balls and m bins, each bin with a certain capacity, how many combinations are there?)

재귀 호출을 메모할 때 엄청난 효율성 차이 (Drastic efficiency difference when memoizing recursive calls)







코멘트