PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000301. Ba phép đếm theo mặt nạ bit (SOS Bit Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ba phép đếm theo mặt nạ bit (SOS Bit Problem)

SOS Bit Problem

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Given a sequence of nn positive integers x1,x2,…,xnx_1,x_2,\ldots,x_n, consider each entry x=xix=x_i and all entries y=xjy=x_j with 1≤j≤n1\le j\le n.

Compute three separate counts:

  • The number of entries yy satisfying (x∣y)=x(x\mid y)=x, where ∣\mid denotes bitwise OR.
  • The number of entries yy satisfying (x&y)=x(x\mathbin{\&}y)=x, where &\mathbin{\&} denotes bitwise AND.
  • The number of entries yy satisfying (x&y)≠0(x\mathbin{\&}y)\ne0.

Count each position separately, including repeated values. The position j=ij=i is also considered.

Input

The first line contains an integer nn, the length of the sequence.

The second line contains nn integers x1,x2,…,xnx_1,x_2,\ldots,x_n in order.

Output

Print nn lines of three counts in the specified order, following the input order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤xi≤1061\le x_i\le10^6 (for every 1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le200000; 1≤xi≤10231\le x_i\le1023 (for every 1≤i≤n1\le i\le n).
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; 1≤xi≤1061\le x_i\le10^6 (for every 1≤i≤n1\le i\le n).

Example 1

Input

5
3 7 2 9 2

Output

3 2 5
4 1 5
2 4 4
1 1 3
2 4 4

Explanation

The five entries are 3,7,2,9,23,7,2,9,2; the two positions containing 22 count separately.

  • For x=3x=3, OR equals 33 at positions 1,3,51,3,5; AND equals 33 at 1,21,2; AND is nonzero at all five positions. The result is 3 2 5.
  • For x=7x=7, OR equals 77 at 1,2,3,51,2,3,5; AND equals 77 at 22; AND is nonzero at all five positions. The result is 4 1 5.
  • For x=2x=2, OR equals 22 at 3,53,5; AND equals 22 at 1,2,3,51,2,3,5, which are also the positions with nonzero AND. The result is 2 4 4.
  • For x=9x=9, only position 44 gives OR equal to 99 or AND equal to 99; AND is nonzero at 1,2,41,2,4. The result is 1 1 3.
  • The final entry is also 22, so its result is 2 4 4.