missionary_69's blog

By missionary_69, history, 6 years ago, In English

You are given a rectangular board of A rows where each row contains B square shaped boxes.
Each square box has a unique integer written on it in the row major order (starting from 1 to A* B). The top left square box has value 1 and bottom right square box has value A*B.
You are asked to cut the board into exactly C parts such that each square box is present in exactly one part.
Note: It is not necessary that each cut starts from the boundary of the board.
Some possible ways are:

  1. A = 3, B = 3, C = 2
  2. A = 2, B = 2, C = 2:

Find the number of different ways in which you can have C parts of the rectangular board. Two ways are considered different if there is at least one part obtained in one way whose identical part is not present in the other way.

1 <= A <= 15
1 <= B <= 15
1 <= A*B <= 15
1 <= C <= A*B

Sample Input: A = 2, B = 2, C = 2

Sample Output: 6

I was asked this question in a coding round. The recurrence might be related to Stirling numbers of the second kind. Any ideas?

Full text and comments »

  • Vote: I like it
  • -18
  • Vote: I do not like it

By missionary_69, history, 6 years ago, In English

You are given a tree with A nodes and A-1 edges which is rooted at 1.
There are C queries and for each query you are given two integers d (the node number) and e and you have to find the maximum value when e is xor'ed with any of the ancestors of d or d itself.
Formally, find the maximum value which can be obtained when e is XOR'ed with any node in the path from d to root. XOR is bitwise XOR operator.

2 <= A <= 100000
Tree given in the form of an array B (one indexed) with A-1 elements where B[i] denotes parent of i+1 th node.
1 <= C <= 300000
1 <= D[i] <= A — node number d
1 <= E[i] <= 300000 — the number to be XOR'ed e

Input
A = 8
B = [1, 1, 2, 2, 3, 3, 1]
C = 5
D = [2, 3, 5, 6, 8]
E = [1, 1, 5, 4, 10]

Output
[3, 2, 7, 7, 11]

Full text and comments »

  • Vote: I like it
  • -17
  • Vote: I do not like it