There should be a feature for the users at CodeChef to be able to delete their posts which are marked as closed, deleted, duplicate etc so that they can help keeping the forum free from all this useless posts.
A feature request
Dijkstar's Algo to find shortest path from 1 to N
In The Dijkstra's algo we need to extract the minimum of D where D is the distance from node 0. But as D gets updated with the distance randomly how could we extract the minimum. Eg: Let at an instance we have the below array and we have used the node 0(its distance to itself is always 0) and accordingly its adjacent node2 and node5 get updated with distance from node 0. D: 0 INF 3 INF INF 1 INF
If we use O(n) then ultimately we are using O(n^2) as we will be updating the adjacent of node 5 and again extracting the minimum by using O(n). Is there a better approach to extract the minimum??
best way to check if a point lies inside a triangle or not
Given the coordinates of 3 vertices of a triangle and a point. What is the most accurate and efficient way to determine if the given point is inside the triangle?
How to be the top contributor on codechef?
What is top contributor and how to be the top contributors on CodeChef?
H_GAMES- Editorial
Author:mesksr
Difficulty
Cakewalk
PREREQUISITES
Math
Problem
There are N bulb, numbered from 1 to N. All are initially off. People numbered from 1 to N go and toggle the state of the bulbs, which are there multiple.
Explanation: Each bulb is toggled by all its divisors. We know that all perfect square number have odd number of divisors and rest of the numbers have even number of divisors. If the bulb is initially off, after even number of toggles, it will become off. Similarly, after odd number of toggles, it will become on. So, if N is the input, we just have to calculate the number of perfect squares less than or equal to N. Note : We need to take long long int to pass all the test cases.
DIV-Editorial
Author:dhruva997
Difficulty
Cakewalk
PREREQUISITES
Simple Math
Problem
Given a natural number, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number.
Explanation: This problem has very simple solution, we all know that for any number ‘num’ all its divisors are always less than and equal to ‘num/2’ and all prime factors are always less than and equal to sqrt(num). So we iterate through ‘i’ till i<=sqrt(num) and for any 'i' if it divides 'num' , then we get two divisors 'i' and 'num/i' , continuously add these divisors but for some numbers divisors 'i' and 'num/i' will same in this case just add only one divisor , e.g; num=36 so for i=6 we will get (num/i)=6 , that's why we will at 6 in the summation only once. Finally we add one as one is divisor of all natural numbers.
HEX- Editorial
Author:karn7
Difficulty
Cakewalk
PREREQUISITES
Simple Math
Problem Given a decimal number, we have to find its corresponding Hexadecimal equivalent.
Explanation:
Question was simple, all you need to do is convert decimal number to hexadecimal. It can be done by using arrays .All you need to do is store ASCII code of converted hexadecimal. And then print in reverse order. For Example: 47 in the Decimal number system can be represented as 2F in the Hexadecimal number system.
For those who don’t know array ,this can be done by storing ASCII code in one large no in 3 digit format (since highest value of ASCII code we need to save is 102 which is F) so that each 3 digit taken from right represent a ASCII value.
TERVEC - Editorial
PROBLEM LINK:
Author:Pavel Sheftelevich
Tester:Kevin Atienza
Editorialists:Suhash Venkatesh and Pushkar Mishra
DIFFICULTY:
Hard
PREREQUISITES:
Hadamard matrices, Sylvester's construction, Paley construction, Williamson construction
PROBLEM:
Given a vector $a$ of length $N^2$ (consisting of only $+1$ and $-1$), we apply a TVT transformation on it to get a new vector $b$ also of length $N^2$. The TVT transformation is applied as follows:
$$b[i] = \sum_{k=0}^{N^2-1} a\left[ (i \bmod N) \times N + (k \bmod N) \right] \times a\left[ \left\lfloor\frac{i}{N}\right\rfloor \times N + (k \bmod N) \right]$$
We say that the vector $b$ is good if $b[i] = N^2$ ( if $i \bmod N = \left\lfloor\frac{i}{N}\right\rfloor$), $0$ otherwise. Given $N$, find if there exists a vector of size $N^2$ (consisting of only $+1$ and $-1$) which becomes good after applying the TVT transformation.
EXPLANATION:
Before we proceed with the individual subtasks, let's first try to simplify the given equation. Notice that we iterate over $k$ from $0$ to $N^2-1$, and we have an inner ($k \bmod n$) which can only take values from $0$ to $N-1$. Therefore we can reduce the equation as:
$$b[i] = N \times \sum_{k=0}^{N-1} a\left[ (i \bmod N) \times N + (k \bmod N) \right] \times a\left[ \left\lfloor\frac{i}{N}\right\rfloor \times N + (k \bmod N) \right]$$
Also, notice that when $i \bmod N = \left\lfloor\frac{i}{N}\right\rfloor$, the equation becomes:
$$b[i] = N \times \sum_{k=0}^{N-1} a\left[ (i \bmod N) \times N + (k \bmod N) \right]^2 = N^2$$
Hence, we only need to worry about the case where $i \bmod N \neq \left\lfloor\frac{i}{N}\right\rfloor$ and solve for the elements of $a$ by equating $b[i] = 0$. Now, let us define $N$ vectors $A_{0}$, $A_{1}$, ..., $A_{N-1}$ each of length $N$, such that $A_{m}$ = ($a[m*N]$, $a[m*N + 1]$, ..., $a[m*N + (N-1)]$), $0 <= m <= N-1$. It is easy to see that on simplifying the above equations of the form $b[i] = 0$, we get $N\times (N-1)/2$ distinct equations of the form $\vec{A_{i}}\cdot \vec{A_{j}} = 0$ (dot product, i != j). Getting to this step is simple and is left as an exercise to the reader.
Now, the problem reduces to finding $N$ vectors (each of length $N$) which are mutually orthogonal and whose entries can only be +1 or -1. After a bit of googling, it is easy to find that this problem is exactly the same as generating a Hadamard matrix of order $N$. Further, you can also infer that a Hadamard matrix of order $N$ exists only if N = 1, 2 (or) 4*k (k > 0). This can be found here on OEIS.
Subtask 1
For this subtask, $N <= 8$. Hence, we need to generate Hadamard matrices of order 1, 2, 4 and 8 for this subtask. For $N = 1$ and $N = 2$, the answer is trivial (sample cases). For $N = 4$, we can solve it by brute force. There are a total of $N^2$ = 16 variables, each of which can be +1 or -1. Hence, we can try all $2^{16}$ combinations and check which of them is valid. Given a set of $N^2$ values, we can check if it forms a good vector in $\mathcal{O}(N^3)$. Also, for $N = 4$ it is very easy to generate a solution by hand using pen & paper. Now, to pass this subtask we only need to find the answer for $N = 8$.
Sylvester's construction:
If we have a Hadamard matrix of order $N$, then this method can generate a Hadamard matrix of order $2^{k}*N$ (k > 0). There is also a Hadamard conjecture that given two Hadamard matrices of orders $X$ and $Y$, a Hadamard matrix of order $X*Y$ can be constructed by considering the Kronecker product of the two matrices. Let $M_{N}$ denote a Hadamard matrix of order $N$. For passing this case, consider $M_{8} = M_{2} \otimes M_{4}$. According to Sylvester's construction, given $M_{N}$, we can generate $M_{2N}$ in the following way:
$$M_{2N} = \left[ \begin{array}{ccc} M_{N} & M_{N} \\ M_{N} & -M_{N} \end{array} \right], \text{since } M_2 = \left[ \begin{array}{ddd} 1 & 1 \\ 1 & -1 \end{array} \right]$$
Thus, we can construct $M_{8}$ from $M_{4}$. Note that $M_{4}$ could have also been constructed from $M_{2}$ using this method. You can read about this method here. This will pass the first subtask.
Subtask 2
For this subtask, $N <= 30$. Hence, in addition to the first subtask we also need to generate Hadamard matrices of orders 12, 16, 20, 24 and 28. We only need to calculate $M_{12}$, $M_{20}$ and $M_{28}$. We can calculate $M_{16}$ and $M_{24}$ using the method explained in subtask 1. $M_{16} = M_{2} \otimes M_{8}$ and $M_{24} = M_{2} \otimes M_{12}$.
Paley construction
Paley construction is a method of constructing Hadamard matrices using finite fields. The Paley construction method uses quadratic residues in a finite field $Z(p)$, where $p$ is the power of an odd prime number. There are 2 cases. If $p \equiv 1 \mod 4$, then a Hadamard matrix of order $2*(p+1)$ can be constructed, and if $p \equiv 3 \mod 4$, then a Hadamard matrix of order $(p+1)$ can be constructed. Let $Q$ be the Jacobsthal matrix for a given odd prime power $p$.
Paley construction I: If $p \equiv 3 \mod 4$, then $H=I+\begin{bmatrix} 0 & j^T\\ -j & Q\end{bmatrix} $ is a Hadamard matrix of order $(p+1)$. Here $j$ is the all 1 column vector of length $p$ and $I$ is the $(p+1)*(p+1)$ identity matrix.
Paley construction II:
If $p \equiv 1 \mod 4$, then the matrix obtained by replacing all $0$ entries in
$\begin{bmatrix}
0 & j^T\\
j & Q\end{bmatrix}
$
with the matrix
$\begin{bmatrix}
1 & -1\\
-1 & -1\end{bmatrix}
$
and all entries $±1$ with the matrix
$\pm\begin{bmatrix}
1 & 1\\
1 & -1\end{bmatrix}
$
is a Hadamard matrix of order $2*(p+1)$
You can read more about Paley construction here. We are yet to discuss how to find the Jacobsthal matrix $Q$. Let's split the Jacobsthal construction also into 2 cases: (1) $p$ is an odd prime, (2) $p$ is the power of an odd prime. It is easy to see that we only need to solve case (1) to pass this subtask => 12 = (11+1) and 11 mod 4 = 3, 20 = (19+1) and 19 mod 4 = 3, 28 = 2*(13+1) and 13 mod 4 = 1. All of 11, 13 and 19 are odd primes.
For an odd prime $p$, the field under consideration is $Z(p)$ and the elements of the field can be given by [$0$, $1$, $2$, ..., $p-1$]. Now, to construct a Jacobsthal matrix we need to calculate the quadratic character $χ(i)$, for all $0 <= i <= p-1$. $χ(0) = 0$, $χ(a) = 1$ if $a = b^2$ for some non-zero finite field element $b$, and $χ(a) = −1$ if $a$ is not the square of any finite field element. It is easy to see that $χ(i)$ can be computed for all $1 <= i <= p-1$ in $\mathcal{O}(p^2)$.
Pseudo code:
for i = 0..(p-1): chi[i] = -1; chi[0] = 0; for i = 1..(p-1): chi[(i*i) % p] = 1;
Now, the Jacobsthal matrix $Q$ of order $p$ can be constructed as : $Q_{i,j} = χ((i-j+q)\bmod q)$. Note that, for an odd prime $p$, the Jacobsthal matrix is circulant (each row is obtained from the row above by cyclic permutation). Using this Jacobsthal matrix, we can construct the Hadamard matrix by Paley construction. This is sufficient to pass the second subtask.
Subtask 3
For this subtask, $N <= 60$. The only special case here is actually $N = 52$, where the Jacobsthal matrix construction needs to be performed under the field $Z(p^2)$ instead of $Z(p)$. 52 = 2*(5^2 + 1) and 25 mod 4 = 1. For all other values of $N <= 60$, the methods used in subtasks 1 and 2 will suffice to find the solution.
Now, let's try to find the Jacobsthal matrix for a general case where $p$ is the power of an odd prime. More specifically, $p$ = $q^m$ and $q$ is an odd prime. We can see that $Z(q^m)$ is an extension field of $Z(q)$ obtained by adjoining a root of an irreducible quadratic. Different irreducible quadratics produce equivalent fields. We can write $Z(q^m)$ as $Z(q)[X] / (f(X))$ for some irreducible polynomial $f(X)$ in $Z(q)[X]$ of degree $m$.
Let us assume that the irreducible quadratic is $a_{m} * X^m + a_{m-1} * X^{m-1} + ... + a_{1} * X + a_{0}$, and let $t$ be any root of this polynomial => $a_{m} * t^m + a_{m-1} * t^{m-1} + ... + a_{1}*t + a_{0} = 0$. Using this, we can write the $q^m$ elements of $Z(q^m)$ as $c_{m-1} * t^{m-1} + c_{m-2} * t^{m-2} + ... + c_{1} * t + c_{0}$, where $0 <= c_{i} <= (q-1)$. Note that there may be several irreducible polynomials and we can choose any of them. All of them produce equivalent fields.
For example, let us consider the field $Z(9)$. Let $X^2 + X − 1$ be an irreducible polynomial over the given field, and let $t$ be any of it's roots. We have $(t^2 + t - 1 = 0) => (1)$. The 9 elements of $Z(9)$ can be written as {(0*t+0), (0*t+1), (0*t+2), (1*t+0), (1*t+1), (1*t+2), (2*t+0), (2*t+1), (2*t+2)}. Now, lets take some element say (t+1) and try to square it. (t+1)^2 mod 3 = (t^2 +2*t + 1) = (-t + 1) + 2*t + 1 {from (1)} = (t + 2) mod 3. Therefore, $χ(t+2) = 1$ since it can be expressed as the square of some element from the field. Similarly, (t+2)^2 mod 3 = (t^2 +4*t + 4) = (-t + 1) + 4*t + 4 {from (1)} = (3*t + 5) = 2 mod 3. Therefore, $χ(2) = 1$.
We also infer here that on squaring any element of $Z(q^m)$, $(c_{m-1} * t^{m-1} + c_{m-2} * t^{m-2} + ... + c_{1} * t + c_{0})^2$, we can reduce it to some other element of the field ($d_{m-1} * t^{m-1} + d_{m-2} * t^{m-2} + ... + d_{1} * t + d_{0}$) using the irreducible polynomial. We have shown this for $m=2$ (considering the field $Z(3^2)$). In fact, we'll show later that for this problem we never have to go beyond $m=2$. For this subtask, $N = 52$ and $p = 25 = 5^2$. Here is a code snippet for $m=2$.
Pseudo Code:
List get_square_residues(q, a1, a0) { /* (X^2 - a1*X - a0) is the irreducible polynomial. If 't' is a root, then t^2 = a1*t + a0. */ List chi(q*q); chi.fill(-1); chi[0] = 0; for (i = 0 to q-1) { for (j = 0 to q-1) { if (i + j == 0) continue; /* We need to compute (i*t + j)^2 = t^2 * i*i + t * 2*i*j + j*j = (a1*t + a0) * i*i + t * 2*i*j + j*j = t * (a1*i*i + 2*i*j) + (a0*i*i + j*j) */ r1 = (a1 * i * i + 2 * i * j) % q; r2 = (a0 * i * i + j * j) % q; if (r1 < 0) r1 += q; if (r2 < 0) r2 += q; chi[q*r1 + r2] = 1; } } return chi; }
Here, note that we are just indexing the element (t*r1 + r2) with (q*r1 + r2) so that we can represent all the $q^2$ elements in a single list easily. Again, this code is only for $m=2$ which is sufficient for solving this problem. Computing the quadratic residues for $m>=3$ is also fairly trivial as already explained and is left as an exercise to the reader.
There are several methods to compute the irreducible polynomial, but we only encounter $Z(5^2)$ and $Z(7^2)$ in this problem. We can simply use a pre computed Conway polynomial from here. The final step to solve this subtask is to compute the Jacobsthal matrix.
Pseudo code:
void fill(Q, base_i, base_j, q, m, chi) { /* 'Q' is the original Jacobsthal matrix that we want to fill. 'chi' array denotes the quadratic character and has length q^m. (base_i, base_j) denotes the top left corner of the (q^m * q^m) sub-matrix that we want to fill in the current function call. */ if (m == 0) { // base case - We just need to fill a 1*1 sub-matrix. Q[base_i][base_j] = chi[0]; return; } for (i = 0 to q-1) { for (j = 0 to q-1) { block_no = (j - i + q) % q; chi_new = chi.sublist(q * block_no, q^(m-1)); // a.sublist(i, n) returns the sub-array {a[i], a[i+1], ..., a[i+n-1]} fill(Q, i * q^(m-1), j * q^(m-1), q, m-1, chi_new); } } }
We call fill(Q, 0, 0, q, m, chi)
to fill $Q$. For a given $p = q^m$, we essentially treat it as $q * q$ sub-matrices each of order $q^{m-1}$. We try to fill this in a circulant fashion, similar to the case where $p$ is an odd prime. We do this recursively until all elements of the matrix $Q$ are filled. We see that the time taken $T(m) = q^2 * T(m-1)$. This gives us $T(m) = (q^m)^2 = p^2$. Therefore, the overall complexity is $\mathcal{O}(N^2)$. This will pass the third subtask.
Subtask 4
This is the final subtask and $N <= 100$. Again, the only special case here is $N=92$. For all other values of $N$, we can use the methods used in the first 3 subtasks to generate the Hadamard matrices. For $N=92$, we can note that Paley construction cannot be used. We need to use some other technique.
Williamson construction
If there exist 4 matrices $A$, $B$, $C$ and $D$, each of order $n$, such that they are all circulant, symmetric matrices (and hence commutative) and $A^2 + B^2 + C^2 + D^2 = 4nI_{n}$, then we can construct a Hadamard matrix of order $4*n$ in the following way.
$$H = \left[ \begin{array}{eee} +A & +B & +C & +D \\ -B & +A & +D & -C \\ -C & -D & +A & +B \\ -D &+ C & -B & +A \end{array} \right]$$
More details about the construction can be found here. Since $A$, $B$, $C$ and $D$ are all circulant matrices, they can be represented by just the first row, and all subsequent rows can be obtained by a cyclic rotation of the previous row. There are ways to compute this, but by doing a bit of search you can find them online. :)
$$A_{0,i} = [1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1]$$ $$B_{0,i} = [1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1]$$ $$C_{0,i} = [1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1]$$ $$D_{0,i} = [1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1]$$
Thus, we see that all the cases can be solved using the above methods and the complexity of all methods is $\mathcal{O}(N^2)$.
P.S. This is a very interesting problem and can be solved in other ways too. If you have used any other method, feel free to mention it in the comments. :)
COMPLEXITY:
$\mathcal{O}(N^2)$ per test case.
SAMPLE SOLUTIONS:
SRTX16B - Editorial
PROBLEM LINK:
Author:Suraj Kumar
Tester:Priyanshu Jain
DIFFICULTY:
EASY
PREREQUISITES:
Matrix form of Fibonacci numbers, Binary exponentiation.
PROBLEM:
You just need to find the nth Fibonacci number modulo 10^9+7.
EXPLANATION:
It is a 2x2 matrix A with A[0][0]=1, A[0][1]=1, A[1][0]=1, A[1][1]=0. With this matrix form one can calculate the nth
Fibonacci just by raising the matrix to the power n. A[0][1] will give you the nth Fibonacci number.
How to find the answer modulo 10^9+7.
Use the Binary Exponentiation technique. If the number becomes greater than 10^9+7 after the multiplication
then the elements of the matrix is replaced by their modulo 10^9+7.
Click here for more details.
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
SRTX16C - Editorial
PROBLEM LINK:
Author:Suraj Kumar
Tester:Priyanshu Jain
DIFFICULTY:
EASY
PREREQUISITES:
Arrays
PROBLEM:
Given an array of 0s and 1s. Traverse it from left end to right end by jumping on only zeroes. Jumping here
means that you can skip either 2, or, 1 or 0 number of zeroes. And also count the minimum number of such
jumps taken.
EXPLANATION:
We will try to simulate the whole scenario. We will traverse the array and keep a variable which will tell the
current position of Jack. We will also keep a count to keep the number of jumps taken.
Now starting at the first leaf. So, our current pointer will keep a value 0, since we start from first leaf which is at
index 0. Our count will be initialized to 0.
We can jump either one step, or two, or three in one chance. So, it will always be better to jump three blocks if
possible. So, we check for this condition. We can jump if the value of 3 rd element from the current value in the
array is 0 (magical leaf). If we cannot jump three blocks, then we try to jump two blocks. Jump if possible, else
check if you can jump at least a single step. If none of these three jump types is possible that means that you
cannot take a jump, and hence it is not possible to cross the river. It can be clearly seen that this condition
happens only when we have three or more consecutive ones (“pseudo-magical leaves”) in our array. And we can
break from whole loop and print “Never Give Up”.
With every jump taken, we will increment our jump counter by one. And also update our current position to
whatever position we jumped onto.
What will be the terminating condition for our simulation? Either we will find a position from where it is not
possible to jump (three or more consecutive ones). Or we have reached the final leaf, (i.e current == n-1).
PSEUDO-CODE:
…
current = 0, count = 0
flag = false
a[n] = a[n+1] = 1
while current < n-1 :
if a[current + 3] == 0 then:
current = current + 3
count = count + 1
else if a[current + 2] == 0 then:
current = current + 2
count = count + 1
else if a[current + 1] == 0 then:
current = current + 1
count = count + 1
else : //Jump is not possible, indicate it by the flag
flag = true
if flag
print : “Never Give Up\n”
else
print : count “\n”
It should be noted that we are accessing the array elements at index more than n. Make sure that your array is
large enough. And these last elements are initialized to 1.
For more clarity see the solutions given below.
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
SRTX16D - Editorials
PROBLEM LINK:
Author:Rishabh Gupta
Tester:Priyanshu Jain
DIFFICULTY:
EASY-MEDIUM
PREREQUISITES:
Matrix, Flood-Fill Algorithm.
PROBLEM:
Given a NxM matrix containing paths and walls, a player with starting co-ordinates, exit point co-ordinates, and
the co-ordinates of starting point of the flood. Check if player or the water reaches first at the exit point.
QUICK EXPLANATION:
Apply flood-fill algorithm from both player and water with the modification of adding a counter to count the number
of steps taken to reach the exit point.
EXPLANATION:
We will apply a modified version of flood-fill algorithm.
A good tutorial on flood-fill can be found here. And the Wikipedia link is here.
We will apply our flood-fill something like this:
bool flood(int i, int j, int dest_i, int dest_j)
{
if(mat[i][j] == -1) // a wall
return false;
if(i == dest_i && j == dest_j)
return true;
return flood(i+1, j, dest_i, dest_j) || flood(i-1, j, dest_i, dest_j) || flood(i, j+1, dest_i, dest_j) || flood(i, j-1,
dest_i, dest_j);
}
This code only checks if the point (dest_i, dest_j) is reachable or not.
Notice that there is no boundary condition, i.e. checking if we reached any edge or corner of the matrix. For this
we will be surrounding our matrix with wall. And take the size of matrix as (N+2)x(M+2), for accommodating the
walls at the edges.
For example, if our matrix input was:
0 0 X 0
0 0 X 0
0 X X X
0 0 0 X
We will save it as:
X X X X X X surrounding walls
X 0 0 X 0 X
X 0 0 X 0 X
X 0 X X X X
X 0 0 0 X X
X X X X X X
This will also ensure 1-based indexing.
Now, we will modify it by adding a counter on every call.
void flood(int i, int j, int dest_i, int dest_j, int cnt)
{
if(mat[i][j] == -1) //a wall
{ //do nothing
return;
}
if(matrix[i][j] == 0 || matrix[i][j] > cnt)
{
matrix[i][j] = cnt;
if(i == dest_i && j == dest_j)
{
return;
//no need to flood more
}
else
{
cnt++;
flood(i+1, j, dest_i, dest_j, cnt);
flood(i-1, j, dest_i, dest_j, cnt);
flood(i, j+1, dest_i, dest_j, cnt)
flood(i, j-1, dest_i, dest_j, cnt);;
}
}
}
We will flood the matrix two times, once for water, and once for the player. This will give us the shortest distance
it will take for both water and player to reach the exit point. Comparing these values will give us the answer.
For more clarity and the implementation see the solution given below.
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
SRTX16E - Editorials
PROBLEM LINK:
Author:Priyanshu Jain
DIFFICULTY:
MEDIUM
PREREQUISITES:
DP, Math etc.
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
What are karma points?
I am new to this karma system, Can you explain me why is it called so? What has karma points got to do with contribution?
SRTX16A - Editorials
PROBLEM LINK:
Author:Priyanshu Jain
DIFFICULTY:
CAKEWALK
PREREQUISITES:
Math
QUICK EXPLANATION:
All the chocolate should be distributed evenly so for this we do number of chocolates divide by number of people and reminder will be chocolates that poppy has to eat.
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
WA in EQGIFTS Problem on IARCS. Pls help.
I was doing this problem on iarcs. The problem is basically a variation of knapsack. Here we require to break set in two parts such that the difference between the sum of parts is minimum. I tried it using top down approach of dp but it is only getting 90 out of 100 points.Couldn't figure out the mistake. My solution is here.
please help me out.
Unable to solve QRK04 Problem
What is the approach to solve the problem QRK04 ? Problem link : www.codechef.com/QCDJ2016/problems/QRK04
GIFTCHEF - Editorial
PROBLEM LINK:
DIFFICULTY:
Easy
PREREQUISITES:
KMP, dynamice programming
PROBLEM:
Given two strings S and F, how many ways there are to choose arbitrary number of non-overlapping substrings of S so that each substring is equal to F. output the answer modulo 109 + 7.
EXPLANATION:
let's say the length of string S is N, and the length of string F is M.
the solution which we are going to describe will consist of two parts, the first part is to calculate an array A which we are going to define below then the second part is make use of that array to calculate the final answer.
what is that array A? it's an array of length N such that each element can be either 0 or 1, if the substring of length M which ends at i is equal to string M then Ai is equal to 1, otherwise it's equal to 0.
how to calculate this array? this is a standard problem known as "string matching problem" which can be done by various algorithms, in particular it can be done in O(N+M) using KMP algorithm.
now after we calculate that array the problem is reduced to calculating the number of sub-sequences of A such that all elements of the sub-sequence are equal to 1 and no two elements of the sub-sequence have distance from each other less than M (otherwise those two elements will correspond to overlapping substrings of string S)
to calculate the number of sub-sequences we will use dynamic programming:
let Fi means the number of sub-sequences of first i elements which have value 1 and Ai is selected.
how to calculate Fi? by definition Ai is the last element of all sub-sequences counted in Fi so where is the second-last element? it could be anywhere that have distance from i at least M so Fi = segma k from 1 to i-M (Fk) + 1, we added 1 because the sub-sequence that consist of only Ai (so there's no second last element) should be counted. note that if Ai is 0 then Fi should be 0 because we only select elements of value 1.
calculating each value of F takes O(N) steps, and we need to calculate O(N) values, so overall complexity is O(N2), but if we notice that elements inside the sigma are always a prefix of F, then we can use cumulative prefix sums so we can calculate the sigma in O(1) and overall complexity of dynamic programming would be O(N)
SETTER'S SOLUTION
Can be found here.
TESTER'S SOLUTION
Can be found here.
Default value of a variable
What is the initial value of a variable before it being assigned a value? For different variable types, I have different initial values:
- (
unsigned
)int
or (unsigned
)long
orbool
(also arrays): 0 - (
unsigned
)long long
: 1 float
: 0double
: 4.94066e-324long double
: 1.79581e+2383- (
unsigned
orsigned
)char`: (doesn't output anything) double array
: 3.23796e-319string
:(unpredictable(random) value)- ...
So why is default initial value like this for different var type?
DS and Algo + Competitive Programming - All you need
Syllabus
Motivational
- Teen mathletes do battle at algorithm olympics-Gennady Korotkevich(tourist)
- Target in six steps– Petr Mitrichev
- Nikhil Garg’s blog posts
- Interview with Rudradev Basak
Competitive Programming
- 4Clojure
- Codeabbey
- Codechef
- Codeeval
- Codeforces
- Coderbyte
- Codewars
- Codingame
- Facebook Hackercup
- Hackerrank
- Hello World Open
- Internet Problem Solving Contest
- Just another Golf Coding
- Microcorruption
- Topcoder
Problem Sets
- Anarchy Golf
- LightOJ
- Caribbean Online Judge
- CheckIO
- CodeHunt
- Coding Bat
- Google Code Jam - Practise
- Hacker.org
- Mathproblem of the Month - Bilkent University
- PKU Online Judge
- Ponder This!
- Project Euler
- Python Practice Projects
- Sphere Online Judge
- TalentBuddy
- Timus Online Judge
- UVa Online Judge
Articles
- Petr Mitrichev's Blog
- Segment Trees
- Data structure and Algorithm
- Algorithms For Interviews And Competitive Programming
- Getting Started with Competitve Programming
- TopCoder Algorithms Tutorial
- eMAXX [Translate this website using Chrome]
- ACM Solve
- vexorian's blog
- Fushar’s Blog
- Topcoder Match Editorial Archive
- Codechef Editorials
- Project Euler Solutions
Code Repository
Coding Calendar
Toolkit
Algorithm and Data Structures PDFS
- A Field Guide To Genetic Programming
- A Field Guide To Genetic Programming
- Algorithmic Graph Theory
- Algorithms, 4th Edition Robert Sedgewick and Kevin Wayne
- Algorithms and Automatic Computing Machines (1963)B. A. Trakhtenbrot
- Algorithms and Complexity(book)(PDF)
- Algorithms and Data-Structures
- Algorithms Course Materials eff Erickson
- Binary Trees(PDF)
- Clever Algorithms
- Data Structures and Algorithms: Annotated Reference with Examples
- Data Structures Succinctly Part 1, Syncfusion
- Data Structures Succinctly Part 2, Syncfusion
- Elementary Algorithms
- Foundations of Computer Science- Al Aho and Jeff Ullman
- Open Data Structures: An Introduction- Pat Morin
- LEDA: A Platform for Combinatorial and Geometric Computing
- Lectures Notes on Algorithm Analysis and Computational Complexity (Fourth Edition) University of North Texas (PDF)
- Linked List Basics (PDF)
- Linked List Problems (PDF)
- Planning Algorithms
- Problems on Algorithms (Second Edition)
- Programming Pearls
- Purely Functional Data Structures
- Sequential and parallel sorting algorithms
- Text Algorithms
- The Algorithm Design Manual
- The Art of Computer Programming(fascicles, mostly volume 4) - Donald Knuth
- The Design of Approximation Algorithms(PDF)
- The Great Tree List Recursion Problem (PDF)
C++ Books
- C++ Annotations
- C++ Cookbook (PDF)
- C++ GUI Programming With Qt 3
- C++ Succinctly, Syncfusion (PDF, Kindle) (Just fill the fields with any values)
- CS106X Programming Abstractions in C++
- Data Structures and Algorithms with Object-Oriented Design Patterns in C++
- Financial Numerical Recipes in C++ - Bernt Arne Ødegaard
- Game Programming Patterns
- Google's C++ Style Guide
- How To Think Like a Computer Scientist: C++ Version - Allen B. Downey
- Introduction to Design Patterns in C++ with Qt
- Learn C++. (PDF, Online)
- Matters Computational: Ideas, Algorithms, Source Code, by Jorg Arndt (PDF)
- Open Data Structures (In C++) (PDF)
- Software Design Using C++ - Br. David Carlson and Br. Isidore Minerd
- Software optimization resources by Agner Fog
- The Boost C++ libraries
- The Rook's Guide to C++ (PDF)
- Thinking in C++, Second Edition, Vol. 1.(Vol. 2) - Bruce Eckel
- Also see: The Definitive C++ Book Guide and List
It just a beginning will keep on adding here, Bookmark this page
Hope it helps!
Thank you!! :D
Yellow timer on homepage
I don't know whether the timer is picking up wrong information from my browser or it's showing wrong for all but it says that contest starts in 7 hours from now on, when i enter it says 3 hours (approx). This caused me to miss last month's short challenge and lunchtime..