1 / 23

Solving Adhoc and Math related problems

Solving Adhoc and Math related problems. - Abhra Dasgupta. Problem 1.

Download Presentation

Solving Adhoc and Math related problems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Solving Adhoc and Math related problems -AbhraDasgupta

  2. 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.

  3. 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 1st move makes it a multiple of 4, i.e., 1st move is n%4.

  4. Problem 2 You are given four 3-dimensional points, check whether they all lie in the same plane. Input FormatFirst line contains T, the number of testcases.Each test case consists of four lines. Each line contains three integers, denoting xiyizi. Output FormatFor each test case, print YES or NO whether all four points lie in same plane or not, respectively.

  5. 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.

  6. 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 FormatA single line containing a string of digits. Output FormatA single line which is the sum, T % (109+7). Constraints1 ≤ N ≤ 2*105

  7. 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 4 7 7 8 7 47 48 78 78 478 478 So we have (4*102)+((4+(2*7))*101)+((4+(2*7)+(3*8))*100)

  8. 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

  9. 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 sub-sequence of the given string. Input FormatA single line containing a string of digits. Output FormatA single line which is the sum, T % (109+7). Constraints1 ≤ N ≤ 2*105

  10. 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*100 + 3*101 + 3*102 + 1*103)) + (x*(1*100 + 4*101 + 2*102)) + (y*(4*100 + 4*101)) + (z*(8*100)) • So let us rewrite it in a good way • Sum = (w*(20)*(113)) + (x*(21)*(112)) + (y*(22)*(111)) + (z*(23)*(110))

  11. 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

  12. Problem 5 You are given a list of N numbers. You can obviously form 2N-1 non empty sub-lists. Consider the sum of all sub-lists, let them be S1, S2, …, Sk ; where k=2N-1. Now you task is to return special_sum defined as Σ 2Si . Input FormatThe 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 FormatPrint special_summodulo (109 + 7). Constraints1 ≤ N ≤ 1050 ≤ ai ≤ 1010 , where i ∈ [1 .. N]

  13. Solution 5 Let us denote the special_sum of list A asf(A). We know f(∅)=1. IfS’ = S∖{x} for some x ∈ S, then f(S) = f(S′)⋅(1+2x) 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 2x). Therefore, one readily sees that f(S) = ∏x∈S(1+2x).

  14. 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 Nth 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 <= 103 N <= 1015

  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.

  16. Problem 7 Given a number N, you must return the Nth Fibonacci number. Input Format One integer N. Output Format Output Nth Fibonacci number modulo 109+7 ConstraintsN<=1012

  17. Solution 7 • Let us first look at technique of using exponentiation to find AN 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;

  18. Solution 7 We will use matrix exponentiation to find the Nth Fibonacci number. Consider the following 2 x 2 matrix: A= The Nth Fibonacci number, denoted by FN can be calculated as: AN = 1 1 10 FN+1 FN FN FN-1

  19. 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 FormatThe 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 FormatFor every test case, output “Yes” or “No” (without quotes). Constraints1 <= T <= 5000 1 <= N <= 10121 <= K <= 1012

  20. 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.

  21. 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.

  22. Problem 9 • Their current salaries of N employees are denoted by sequence of N integers A1, A2, A3 … AN . 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 FormatFirst 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 FormatFor each query, print the normalized salary(which is same for everyone in the end) in one line. • Constraints1 ≤ N ≤ 1051 ≤ Q ≤ 1051 ≤ A[i] ≤ 10140 ≤ K ≤ 109

  23. Solution 9 • Without loss of generality we can assume A1,A2,…,AN are in sorted order. • The normalization process is actually finding GCD of all numbers. • Now if we add K to all, the required answer is: • gcd(A1+K, A2+K, ..., An+K) = gcd(A1+K, A2+K-(A1+K), A3+K-(A1+K)..., An+K-(A1+K)) = gcd(A1+K, A2 - A1, A3 - A1,..., An-A1) = gcd(A1+K, G) • where G = gcd(A2-A1, A3-A1..., An-A1) • G can be pre-calcualated and stored.

More Related