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

#PS0000287. Tích các phần tử ngoại trừ chính nó (Product of Array Except Self)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tích các phần tử ngoại trừ chính nó (Product of Array Except Self)

Product of Array Except Self

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

For every position ii in a1,…,ana_1,\ldots,a_n, compute bi=∏1≤j≤n, j≠iajb_i=\prod_{1\le j\le n,\ j\ne i}a_j. Output the results in original order. The intended algorithm is linear and uses no division.

Input

First line: nn. Second line: exactly nn integers a1,…,ana_1,\ldots,a_n.

Output

Print nn integers b1,…,bnb_1,\ldots,b_n on one line.

Subtasks

  • Subtask 1 (20 points): 2≤n≤302\le n\le30; −30≤ai≤30-30\le a_i\le30; every prefix product, suffix product and bib_i fits in [−231,231−1][-2^{31},2^{31}-1].
  • Subtask 2 (30 points): 2≤n≤1000002\le n\le100000; −30≤ai≤30-30\le a_i\le30; every prefix product, suffix product and bib_i fits in [−231,231−1][-2^{31},2^{31}-1]; at least two zeros.
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le100000; −30≤ai≤30-30\le a_i\le30; every prefix product, suffix product and bib_i fits in [−231,231−1][-2^{31},2^{31}-1].

Example 1

Input

4
1 2 3 4

Output

24 12 8 6

Explanation

Omitting 1, 2, 3 and 4 gives 24, 12, 8 and 6 respectively.

Example 2

Input

5
-1 1 0 -3 3

Output

0 0 9 0 0

Explanation

Only omitting the zero at position 3 produces a nonzero product, equal to 9.