- ABHRA DASGUPTA Solving Adhoc and Math related problems.

Slides:



Advertisements
Similar presentations
Discrete Mathematics Math 6A Homework 9 Solution.
Advertisements

Lecture 3: Parallel Algorithm Design
1 Transportation problem The transportation problem seeks the determination of a minimum cost transportation plan for a single commodity from a number.
Recursively Defined Functions
Having Proofs for Incorrectness
Study Guides Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM Mycatstudy.com.
Copyright © 2013, 2010, and 2007, Pearson Education, Inc.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Deterministic Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms.
The number of edge-disjoint transitive triples in a tournament.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
61 Nondeterminism and Nodeterministic Automata. 62 The computational machine models that we learned in the class are deterministic in the sense that the.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Congruence of Integers
Hash Tables1 Part E Hash Tables  
This material in not in your text (except as exercises) Sequence Comparisons –Problems in molecular biology involve finding the minimum number of edit.
Hash Tables1 Part E Hash Tables  
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Hash Tables1 Part E Hash Tables  
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Games, Hats, and Codes Mira Bernstein Wellesley College SUMS 2005.
Mathematics of Cryptography Part I: Modular Arithmetic, Congruence,
Integers Number Theory = Properties of Integers
CSCI 1900 Discrete Structures
9/2/2015Discrete Structures1 Let us get into… Number Theory.
Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM
Mathematics of Cryptography Part I: Modular Arithmetic
Induction and recursion
Computer Arithmetic Nizamettin AYDIN
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Merkle-Hellman Knapsack Cryptosystem Merkle offered $100 award for breaking singly - iterated knapsack Singly-iterated Merkle - Hellman KC was broken by.
Chapter 14 Randomized algorithms Introduction Las Vegas and Monte Carlo algorithms Randomized Quicksort Randomized selection Testing String Equality Pattern.
Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing.
DECIDABILITY OF PRESBURGER ARITHMETIC USING FINITE AUTOMATA Presented by : Shubha Jain Reference : Paper by Alexandre Boudet and Hubert Comon.
Basic Concepts in Number Theory Background for Random Number Generation 1.For any pair of integers n and m, m  0, there exists a unique pair of integers.
Design of Algorithms using Brute Force Approach. Primality Testing (given number is n binary digits)
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem.
Merkle-Hellman Knapsack Cryptosystem
CSC 211 Data Structures Lecture 13
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
© 2004 Goodrich, Tamassia Hash Tables1  
Real numbers In algebra, we work with the set of real numbers, which we can model using a number line. Real numbers describe real-world quantities such.
1 Parrondo's Paradox. 2 Two losing games can be combined to make a winning game. Game A: repeatedly flip a biased coin (coin a) that comes up head with.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Module #9 – Number Theory 1/5/ Algorithms, The Integers and Matrices.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
MA/CSSE 473 Day 06 Mathematical Induction Modular Arithmetic Do question 1 on today's quiz (work with another person)
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
MA/CSSE 473 Day 07 Extended Euclid's Algorithm Modular Division Fermat's little theorem intro.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Equivalence Relations: Selected Exercises
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Lecture 3: Parallel Algorithm Design
More Mathematical Techniques
PEA202 – Lecture #1 NUMBER SYSTEM.
Recursively Defined Functions
More Mathematical Techniques
More Mathematical Techniques
CS203 Lecture 15.
Presentation transcript:

- ABHRA DASGUPTA Solving Adhoc and Math related problems

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 % ( ). 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 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 <  for i in 0 to  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 sub- sequence of the given string. Input Format A single line containing a string of digits. Output Format A single line which is the sum, T % ( ). 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 3 )) + (x*(1* * *10 2 )) + (y*(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 <  for i in 0 to  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 ( ). Constraints 1 ≤ N ≤ ≤ 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 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= The N th Fibonacci number, denoted by F N can be calculated as: A N = F N+1 F N F N F 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 <= <= N <= <= 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 ≤ ≤ Q ≤ ≤ A[i] ≤ ≤ K ≤ 10 9

Solution 9 Without loss of generality we can assume A 1,A 2,…,A N 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(A 1 +K, A 2 +K,..., A n +K) = gcd(A 1 +K, A 2 +K-(A 1 +K), A 3 +K-(A 1 +K)..., A n +K-(A 1 +K)) = gcd(A 1 +K, A 2 - A 1, A 3 - A 1,..., A n -A 1 ) = gcd(A 1 +K, G)  where G = gcd(A 2 -A 1, A 3 -A 1..., A n -A 1 )  G can be pre-calcualated and stored.