# Byte Code 11

date post

07-Apr-2018Category

## Documents

view

214download

0

Embed Size (px)

### Transcript of Byte Code 11

8/3/2019 Byte Code 11

1/22

Bytecode 2011

Problems and Solutions

- P. Suhash Venkatesh

Vikas Kumar MallVenkatesh Basker

8/3/2019 Byte Code 11

2/22

Problem A: Traversing Grid

Given 2 points in 2 dimensional space (xs,ys) and (xd,yd), your task is to find whether (xd,yd) can

be reached from (xs,ys) by making a sequence of zero or more operations.

From a given point (x, y), the operations possible are:

a) Move to point (y, x)

b) Move to point (x, -y)

c) Move to point (x+y, y)

d) Move to point (2*x, y)

Input

The first line of input contains 'T', the number of test cases. 'T' lines follow, one for each test case.

For each test case, the input contains one line denoting the 4 integers xs, ys, xd, yd.

Output

Output 'T' lines, one for each test case. For each test case, output "YES" if (xd,yd) is reachable from

(xs,ys) and "NO" otherwise (quotes for clarity).

Example

Input:

1

1 1 2 2

Output:YES

Constraints:

T (1,2), using the operation (x,y) -> (y,x). Finally use the operation

(x,y) -> (2*x,y) to move from (1,2) -> (2,2).

Time limit: 1s

Solution:

Firstly, note that the operations a, b and c do not change the gcd, and operation d either leaves the

gcd as the same or multiplies the gcd by 2.

Therefore, (xd,yd) can be reached from (xs,ys) only if gcd(xd,yd)=gcd(xs,ys) * 2^k, for some k>=0.

8/3/2019 Byte Code 11

3/22

Now, to prove that this is indeed a sufficient condition, we have to prove that (xd,yd) can be reached

from (xs,ys) if the above condition is satisfied. For this, we will give a construction for the solution:

Keep performing operation d (combined with operation a) till we get gcd(xs,ys)=gcd(xd,yd). Now

we can prove that we can reach (xd,yd) from (xs,ys) using only operations a,b,c.

Let g=gcd(xs,ys)=gcd(xd,yd)

Using the same method of repetitive subtraction for finding the gcd of 2 numbers, we can reduce

both pairs of numbers to (g,0). But now, note that each operation we performed is reversible, that is

if we can move from point P1 to P2, then we can move from point P2 to P1 too. Hence, this provesthat we can reach (xd,yd) from (xs,ys).

Complexity: O(log MAX_COORD)

8/3/2019 Byte Code 11

4/22

Problem B : Finding MinimumYou are given 'n' integers k1, k2, ...., kn and an integer 'x', which satisfy the equation x1

k1 * x2k2 * .....

* xnkn = x. You are also given values a1, a2, ...., an and y1, y2, ...., yn. Your task is to find the least

positive value 'v', that can be taken by the expression: a1*x1y1 + a2*x2

y2 + ... + an*xnyn. Note that x1,

x2, x3, ..... xn are some variables (not necessarily integers), which can only take positive values.

Input

The first line of input contains a single integer 't', denoting the number of test cases.

The first line of each testcase contains two space separated integers 'n' and 'x'.

Next line contains 'n' integers k1, k2, ..., kn.

Next line contains 'n' integers a1, a2, ..., an.

Next line contains 'n' integers y1, y2, ..., yn.

Output

For each testcase output the least positive value 'v' that can be taken by the expression. To avoid

floating point errors, round it off to the nearest integer.

For example, 12.6 is rounded off to 13, and 12.4 is rounded off to 12. To avoid ambiguity, there will

be no test case for which the fractional part of the answer equals 0.5.

Example

Input:

21 4

2

3

3

2 6

1 1

1 1

1 1

Output:

24

5

Constraints:

t

8/3/2019 Byte Code 11

5/22

Time limit: 1s

Solution:

This problem exploits the following property: If the product 'n' quantities is a constant, then their

sum is minimum when all the 'n' quantities are equal (when the quantities are positive). Now, we aregiven an equation of the form: x1

k1 * x2k2 * ..... * xn

kn = x. By making some clever substitutions of

the form x1k1 =X1 , x2

k2 = X2 , ....., xnkn = Xn etc, and by using the above property, the problem can

be solved easily.

Complexity: O(n)

8/3/2019 Byte Code 11

6/22

Problem C : Fun With Inequalities

You are given 'n' inequalities. Each inequality is of one of the following 4 types:

Type 1: x > v

Type 2: x < v

Type 3: x = v

Type 4: x != v

where 'x' is a variable which can only take non-negative integral values.

Your task is to find the maximum number of inequalities which are satisfied for some value of 'x'.

You are also required to find the minimum value of 'x' for which the maximum number of

inequalities are satisfied.

Input

The first line of input contains a single integer 'n', denoting the total number of inequalities.

Each of the next 'n' lines contain 2 space separated integers t i and vi. ti denotes the type of inequality

and vi denotes the value on the right hand side of the inequality.

Output

Output two space separated integers, the first integer denoting the maximum number of inequalities

which are satisfied for some value of 'x', and the second integer denoting the minimum value of 'x'for which the maximum number of inequalities are satisfied.

Example

Input:

4

1 10

2 9

3 7

4 4

Output:

3 7

Constraints:

1

8/3/2019 Byte Code 11

7/22

Solution:

In this question, we are given many inequalities and we are to find the value of x which satisfies the

maximum number of inequalities.

Firstly, for each inequality (of the form x operation v), we only need to consider v-1, v, v+1 as event

points (along with 0).

Now, traverse once from left to right, simultaneously processing each inequality of the form x>v,

and update the corresponding event points.

Similarly, we can do 3 more traversals for the other 3 types of inequalities. If the maximum value of

v was small, then this could simulated using an array. The values of v can be really large, but for

this problem, there can be at-most 3*n different values which can be taken by v. Since the value of

'n' is considerably small, we can map the possible values of v to smaller integers and then simulate.

Complexity: O(N*log N) (Assuming mapping the values takes O(log N) time).

8/3/2019 Byte Code 11

8/22

Problem D : Maximum ProfitChakra is a young and dynamic entrepreneur, who is developing rapidly as a successful hotelier. He

owns the Quickbyte chain of restaurants, 'M' of which are fully functional now. He divides each day

into 'N' time slots. For each time slot 'j', in every restaurant 'i', there are A ij waiters and Bij

customers. Being a quality conscious person, he wants each waiter to handle atmost one customer ina given time slot. Since he is really busy, in a day each restaurant is open only during one of the

time slots. Since the hunger and demand for food varies during the day, the price which the

customer is willing to pay varies, and is given by Cij for a restaurant 'i' during a time slot 'j'.

Given the values of Aij, Bij and Cij, find the maximum profit which Chakra can make in a day.

Input

The first line of input contains an integer 't', denoting the number of test cases.

For each testcase, the first line contains 2 space separated integers 'M' and 'N'.

Each of the next 'M' lines contains 'N' integers. The jth

integer on the ith

line denotes the value of Aij

Each of the next 'M' lines contains 'N' integers. The jth integer on the ith line denotes the value of Bij

Each of the next 'M' lines contains 'N' integers. The jth integer on the ith line denotes the value of Cij

Output

For each test case output one value, denoting the maximum profit which Chakra can make in a day.

Example

Input:

1

2 3

1 2 3

3 2 1

3 2 1

1 2 3

4 5 2

3 1 6

Output:

16

Constraints:

t

8/3/2019 Byte Code 11

9/22

Solution:

For each restaurant, greedily pick the slot which gives the maximum profit. For slot (i,j),

profit=C(i,j)*min(A(i,j)*B(i,j)).

Complexity: O(M*N)

8/3/2019 Byte Code 11

10/22

Problem E: Recover PolynomialsVenkatesh is an expert in mathematics, and loves playing around with polynomials during his free

time. His favourite mathematical equation is pretty obviously: f(x) = a n*xn + an-1*x

n-1 + ..... + a1*x

+ a0. His friend Suhash loves posing challenges to Venkatesh. Once they were discussing a

particular problem at Snacky, which goes as follows:

Suhash would choose an integer 'n' as the degree of the polynomial and give Venkatesh the value of

the polynomial at 'n+1' equally spaced points, i.e he gives Venkatesh integers 'n', 'x0', 'd' and g0, g1,

g2, ..., gn such that: f(x0) = g0, f(x0+d) = g1, f(x0+2*d) = g2, ...... , f(x0+n*d) = gn. Now, Venkatesh is

required to find the polynomial. Since he hates floating point values, he decides to find the

polynomial in coefficient form, modulo a prime number. Can you help Venkatesh find the

polynomial?

Input

The first line of input contains an integer 't', denoting the number of test cases.For each test case, the first line contains 3 space separated integers 'n', 'x0', 'd'. The next line

contains 'n+1' space separated integers g0, g1, g2, ... ,gn.

Output

For each test case out

*View more*