Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Advanced Programming : 2021

Assignment – 5

Due: Monday 23:59 IST, July 19, 2021

Instructions
• Submit two files, one for each problem <cmi id> 1.py and <cmi id> 2.py
• Example: sdatta 1.py and sdatta 2.py

Problem 1
Chino loves to solves puzzles. One day she was playing with a rectangular piece of
paper with integer sides. Chino is fascinated by the square shape because it has lots
of symmetry and she absolutely loves integers because of their simplicity. She was
wondering if she could take the rectangle and cut it into square pieces and suddenly
got the answer that she could cut it into 1 × 1 squares. But this didn’t make her
happy. So she thought of finding the minimum number of cuts needed, to cut the
rectangular piece of paper into a bunch of square pieces. She’s isn’t able to find the
solution as the paper is quite big, so needs your help to find the answer.
Formally, there is an a × b rectangle and at each move, you pick only one rectangle
and cut it into two rectangles of smaller size such that all the sides are still integers.
You need to find the minimum number of moves required to cut the rectangles such
that each piece is a square. To illustrate the cutting procedure, a 2 × 2 square can be
cut into four 1 × 1 in 3 moves, but not 2 moves. But these moves will be a waste as
2 × 2 is already a square.

1
Use the Standard input and output for this problem.
Input:
The only line contains two space separated integers a, b.
Output:
Print a single integer, the minimum number of moves.
Constraints:
1 ≤ a, b ≤ 500.

Sample Input:
5 6
Sample Output:
4
Explanation:
4<5

2
Problem 2
Yui has two necklaces A and B, each containing n beads. Each of these 2n beads has
a non-negative integer written on it.
Mugi gives her the following challenge and as a reward Yui will get lots of sweets.
Yui is allowed do two operations on the necklace A. For the first one, she can choose
0 ≤ r < n and rotate the necklace by r beads in counter-clockwise direction. In the
second operation, she can pick any non-negative integer x and XOR (defined below)
each element of A by x.
So if A initially looked like [a0 , a1 , . . . , an−1 ] then after the rotating by r, it should
look like [ar , ar+1 , . . . , an−1 , a0 , . . . , ar−1 ].
And XOR-ing this with x, should give, [ar ⊕ x, . . . , an−1 ⊕ x, a0 ⊕ x, . . . , ar−1 ⊕ x].
The challenge is to find all the pairs (r, x) such that rotating A by r and then XOR-
ing by x makes the necklace A look exactly same as necklace B. That is find all the
pairs (r, x) such that

bi = a(r+i) mod n ⊕ x for 0 ≤ i < n.

Since Yui is too busy dreaming about the sweets, she needs your help to get through
this challenge.

About XOR:
• Given two non-negative integers x and y, write them in base 2 as x = ki=0 xi ·2i
P

and y = ki=0 yi · 2i , where xi , yi ∈ {0, 1}. Let zi = xi + yi (mod 2). Then


P

x ⊕ y := ki=0 zi 2i . That is we XOR bits at each position!


P

• This operation is commutative, associative and also satisfies x ⊕ x = 0.


• In python you can use the operator ^ (carat/hat) for getting XOR of two non-
negative integers.
• Equivalently you can think of this operation through the isomorphism of Abelian
Groups ϕ : (F2 [x], +) → (N, ⊕), which is informally given by imagining x 7→ 2.

3
Input: Read the input from the file inp.txt
• The first line contains a single integer n
• The second line contains n space separated integers a0 , . . . , an−1
• The last line also contains n space separated integers b0 , . . . , bn−1
Output: Write to the file out.txt
• First line should contain a single integer m, the number of solution pairs
• The next m lines each should contain two space separated integers ri , xi
Constraints:
• 1 ≤ n ≤ 106
• 0 ≤ ai , bi < 230

Sample Input:
3
0 1 2
3 2 1
Sample Output:
1
0 3
Explanation:
Writing in base 2, the two necklaces are 00, 01, 10 and 11, 10, 01. Now, 00, 01, 10;
01, 10, 00 and 10, 00, 01 are all possible rotations of the first necklace. It can be
verified that only the first one when XOR-ed with 11 gives the required solution.

You might also like