Skip to main content

Posts

Digit Dynamic Programming - Digit DP : Part-2

In the last post i discussed few basics required for DP on digits. Now i am going to explain you a  problem so as to relate to what that post meant.  Problem : Given a range \([L,R]\) , how many numbers are there such that its two adjacent digits form a number which is a perfect square. Solution: Problem asks us to count numbers like \(125\) as \(25\) is a prefect square, \(198\) is not valid as there are no two adjacent digits for which the number is a perfect square.  First of all let us discuss some state variables.  \(pos\) : \(pos\) tells us that you are going to place any digit on this position \(isequal\) : Discussed already in previous post, this tells us whether the number formed till now is equal to the prefix of the limiting number starting from \(1\) to \(pos-1\). For example let us count numbers less than \(1269\) then for the status \(126 __\) , \(isequal\) will be \(1\). \(started\) : This tells us whether number formation is started or not. In simpl

Digit Dynamic Programming - Digit DP : Part-1

DP is one of the most important trick used in programming. This article discusses about a variant that can be used to solve problems like how many numbers are there between \(a\) and \(b\) such that they contain a digit \(x\). Now since this one may be easy and you can think of some formula but DP can be used as a very nice approach here. Suppose you have been given a number \(1608\) and you want to enumerate all the numbers which are lesser than or equal to it. This can be done easily using the following process. Let us assume we have four blanks \( ___ ___ ___ ___\) . Now what can be the first digit from left it can be either \(1\) or \(0\). i.e. \(0 ___ ___ ___\) and \(1 ___ ___ ___\) . Now if we have placed \(0\) in the beginning it is a clear observation that we have not made any \(4\) digit number so the second digit can take values \(0\) to \(9\), but if we have placed \(1\) there then the next digit will be always \(0\) to \(6\) or else number will be greater than the re

Sparse Tables Range Query

Range query problems are generally solved by either Binary Indexed Tree or Segment Tree. But sometimes when total queries are very large then you need some another data structure such that query time is almost constant and prefetching or pre-processing time is \(N log (N)\). The data structure is Sparse Tables. Assume a range query problem where \(10^7\) queries will be asked. Problem Statement :  You are given an array of size \(10^5\) and \(10^7\) queries to find the minimum of all the numbers in range \([L..R]\) then segment tree will be too slow, so we need something very fast. In sparse table we use the concept of binary numbers (Binary Lifting to be specific). The concept is to break down the complete linear array in chunks of powers of 2 and then utilize them to solve the queries. For example - let array indexes be \(0,1,2,3,4\) then break the array in chunks of powers of two as below - \(Row_0 : [0..0], [0..1], [0..3]\) \(Row_1 : [1..1], [1..2], [1

Importing your templates in Vim

How does it feels making copy of your coding templates before any coding competition ? Isn't there any method such that if you are making a new c++ file or c file and your template is automatically loaded. It is not a very tough task to just copy your code from github or just make copies of a template file but still here i am going to discuss a one time method so that you don't need to prepare anything before writing your code . An auto command such that your template is automatically imported whenever you make a new file is just awsome. Before we proceed i would like to say that every software or program you use certain events are associated with it. For Vim or any editor opening a file when it does not exist , opening a pre existing file etc. are different events. We need an autocommand for the former one. Vim contains a configuration file which can be edited from the home direcory as gedit .vimrc or vim .vimrc . You have to edit this file to make it perfect for codin