Slides

advertisement

Solving Adhoc and Math related problems

- A B H R A D A S G U P T A

Problem 1

 Suppose you are given a pile of n coins. A player can pick up either 1/2/3 coins. Two players play the game taking moves alternately. The player to pick up the last coin wins the game. Both players play optimally. If player 1 can win print his first move or else print -1.

Solution 1

 In base case if number of coins is <=3 player 1 just picks all coins.

 If n is multiple of 4 player 1 always loses.

 Else player 1 in 1 st move makes it a multiple of 4, i.e.,

1 st move is n%4.

Problem 2

 You are given four 3-dimensional points, check whether they all lie in the same plane.

 Input Format

First line contains T, the number of testcases.

Each test case consists of four lines. Each line contains three integers, denoting x i y i z i

.

 Output Format

For each test case, print YES or NO whether all four points lie in same plane or not, respectively.

Solution 2

Suppose we have 3 points A, B, C, D.

We fix the first point and form 3 vectors say AB, AC, AD; where

AB = B - A

AC = C - A

AD = D - A

Now we find V= AB x AC

V will be perpendicular to the plane of A, B and C.

If D lies in same plane the vector V must be perpendicular to AD. So if AD . V = O, then we could say

A, B, C, D are in same plane.

Problem 3

You are given a string of digits(0-9) of length N. Now each substring of the string is to be considered as a number. You need to find the sum of all substrings of the given string.

Input Format

A single line containing a string of digits.

Output Format

A single line which is the sum, T % (10 9 +7).

Constraints

1 ≤ N ≤ 2*10 5

Solution 3

Let us consider an example to consider the problem.

Given string S=478. S is of length 3, i.e., N=3.

Subsets are: Lets sort for each position and see.

4

7

8

47

78

478

4

7

7

48

78

478

So we have (4*10 2 )+((4+(2*7))*10 1 )+((4+(2*7)+(3*8))*10 0 )

Solution 3

 So the algorithm for string S of length N will be:

Preprocessing:

Temp <- 1

Mod <- 1000000007 for i in 0 to 200000

Pow[i] <- Temp

Temp <- ( Temp * 10 ) % Mod

Main Algorithm:

M <- 0;

Ans <- 0; for i in 0 to n-1

M <- (M + ( (i+1) * (S[i] – ‘ 0 ’ ) )) % Mod

Ans <- ( Ans + ( M * Pow[n-1-i] ) % Mod ) % Mod

Return Ans

Problem 4

You are given a string of digits(0-9) of length N. Now each sub-sequence of the string is to be considered as a number. You need to find the sum of all subsequence of the given string.

Input Format

A single line containing a string of digits.

Output Format

A single line which is the sum, T % (10 9 +7).

Constraints

1 ≤ N ≤ 2*10 5

Solution 4

Suppose you are given a string S=wxyz.

Length of string is 4, so N=4.

Now let us see all possible subsequence in order of thier size.

(1) -> w, x , y, z

(2) -> wx, wy, wz, xy, xz, yz

(3) -> wxy, wxz, wyz, xyz

(4) -> wxyz

Now we actually need to first the weightage of digit at each position and find the sum.

Sum = (w*(1*10 0 + 3*10 1 + 3*10 2 + 1*10 3 )) + (x*(1*10 0 + 4*10 1 +

2*10 2 )) + (y*(4*10 0 + 4*10 1 )) + (z*(8*10 0 ))

So let us rewrite it in a good way

Sum = (w*(2 0 )*(11 3 )) + (x*(2 1 )*(11 2 )) + (y*(2 2 )*(11 1 )) + (z*(2 3 )*(11 0 ))

Solution 4

 So the algorithm for string S of length N will be:

Preprocessing:

Temp_two <- 1 ; Temp_eleven <- 1;

Mod <- 1000000007 for i in 0 to 200000

Pow_two[i] <- Temp_two

Pow_eleven[i] <- Temp_eleven

Temp_two <- ( Temp * 2 ) % Mod

Temp_eleven <- (Temp * 11) % Mod

Main Algorithm:

Ans <- 0; for i in 0 to n-1

Temp <- ( Pow_two[i] * Pow_eleven[n-i-i] ) % Mod

Temp <- ( S[i] * Temp ) % Mod

Ans <- ( Ans + Temp ) % Mod

Return Ans

Problem 5

You are given a list of N numbers. You can obviously form 2 N -1 non empty sub-lists. Consider the sum of all sub-lists, let them be S1, S2, …, Sk ; where k=2 N -1. Now you task is to return special_sum defined as Σ 2 Si .

Input Format

The first line contains an integer N, i.e., the size of list A.

The next line will contain N integers, each representing an element of list A.

Output Format

Print special_sum modulo (10 9 + 7).

Constraints

1 ≤ N ≤ 10 5

0 ≤ a i

≤ 10 10 , where i ∈ [1 .. N]

Solution 5

Let us denote the special_sum of list A as f(A).

We know f( ∅ )=1.

If S’ = S ∖ {x} for some x ∈ S, then f(S) = f(S′) ⋅ (1+2 x ) because the subsets of S can be partitioned into those not containing x (giving f(S′)) and those containing x (where each corresponding summand is multiplied by 2 x ). Therefore, one readily sees that f(S) = ∏ x ∈ S

(1+2 x ).

Problem 6

Suppose you are given a list of all numbers starting from 1 in binary format, the list would be like 1,10,11,100,... and so on, from which we remove any pattern having atleast two consecutive ones and prepare a new list. The new list will be like 1,10,100,101,... and so on. Now if a value N is given you must return the value of the N th element of the list.

Input Format

The first line contains T(<=1000) test cases. The next T lines each contain one integer N(<=10^15) .

Output Format

Output T lines each containing the required answer.

Constraints

T <= 10 3

N <= 10 15

Solution 6

This problem is solved by just writing the value of N in ‘Fibonacci base’.

How to evaluate Fibonacci base?

Find the nearest Fibonacci number(F) less than equal to N.

Subtract f from N and write 1(MSB of output).

Now check for the lower Fibonacci number, if it is greater than

N then print 0 as that bit, else print 1 and reduce the N by the

Fibonacci number.

Repeat the above step till all Fibonacci numbers less than F are used.

Problem 7

Given a number N, you must return the N th

Fibonacci number.

Input Format

One integer N.

Output Format

Output N th Fibonacci number modulo 10 9 +7

Constraints

N<=10 12

Solution 7

Let us first look at technique of using exponentiation to find A N in log(N) time, where A and N are integers.

Power(A,N):

Result <- 1 while (N)

 if (N%2 == 1)

Result *= A

N = N / 2

A = A * A

 return Result;

Solution 7

We will use matrix exponentiation to find the N th

Fibonacci number.

Consider the following 2 x 2 matrix:

A=

A N =

1 1

1 0

 The N th Fibonacci number, denoted by F

N calculated as: can be

F

N+1

F

N

F

F

N

N-1

Problem 8

You’re given a number N and a positive integer K. Tell if N can be represented as a sum of K prime numbers (not necessarily distinct).

Input Format

The first line contains a single integer T, denoting the number of test cases.

Each of the next T lines contains two positive integers, N & K, separated by a single space.

Output Format

For every test case, output “Yes” or “No” (without quotes).

Constraints

1 <= T <= 5000

1 <= N <= 10 12

1 <= K <= 10 12

Solution 8

 We are going to use Goldbach’s Conjecture to solve this problem.

 Goldbach’s Conjecture states that :

Every even integer greater than 2 can be represented as a sum of two prime numbers.

Any number greater than 5 can be represented as a sum of three prime numbers.

Solution 8

 Now since we know the Goldbach’s Conjecture we device our strategy accordingly:

N < 2K: The answer is “No”, because the sum of K primes is at least 2K.

N ≥ 2K and K = 1: The answer is “Yes” if N is prime, and “No” otherwise.

N ≥ 2K, K = 2 and N is even: The answer is “Yes” (by Goldbach’s conjecture).

N ≥ 2K, K = 2 and N is odd: The answer is “Yes” if N − 2 is prime, and “No” otherwise. This is because the sum of two odd primes is even, and the only even prime number is 2, so one of the prime numbers in the sum must be 2.

N ≥ 2K and K ≥ 3: The answer is “Yes”. This is because if N is even, then

N − 2(K − 2) is also even, so it is the sum of two primes, say p and q (by

Goldbach’s conjecture). Thus, N is the sum of the K primes 2, 2, 2, ..., p, q.

And if N is odd, then N − 3 − 2(K − 3) is even, so it is the sum of two primes, say p and q. Thus, N is the sum of the K primes 3, 2, 2, ..., p, q.

Problem 9

 Their current salaries of N employees are denoted by sequence of N integers A

1

, A

2

, A

3

… A

N

.

Manager has decided to take action and make their salaries equal. He uses the following process until all salaries are equal. This method is called as normalization:

 a) Select any two different values from A.

b) Replace larger value with the difference of the two. Difference of two positive integers B and C is defined as |B-C|.

He knows that the final value will always be unique.

Now, Q queries are given. In each query you are given integer K. K is the amount to be added to everyone’s salary as bonus, before the normalization.

Input Format

First line contains, N and Q, the number of employees and the number of queries. Next line contains N space separated positive integers denoting the array A. Next Q lines contain queries. Each query consists of one integer per line denoting K.

Output Format

For each query, print the normalized salary(which is same for everyone in the end) in one line.

Constraints

1 ≤ N ≤ 10 5

1 ≤ Q ≤ 10 5

1 ≤ A[i] ≤ 10 14

0 ≤ K ≤ 10 9

Solution 9

Without loss of generality we can assume A

1 are in sorted order.

,A

2

,…,A

N

The normalization process is actually finding GCD of all numbers.

Now if we add K to all, the required answer is:

 gcd(A

1

+K, A

= gcd(A

1

= gcd(A

1

= gcd(A

1

2

+K, ..., A n

+K, A

+K, A

2

2

+K, G)

+K-(A

- A

1

1

, A

3

+K)

+K), A

- A

1

3

+K-(A

, ..., A n

-A

1

1

+K)..., A

) n

+K-(A

1

+K)) where G = gcd(A

2

-A

1

, A

3

-A

1

..., A n

-A

1

)

G can be pre-calcualated and stored.

Download