Why m i getting NZEC (Non Zero Exit Code) for my program?
Why do I get an NZEC?
Codechef username
Can i change my codechef username ?
RECIPE - Editorial
PROBLEM LINKS
DIFFICULTY
EASY
EXPLANATION
The problem here is divide all numbers by some constant so that the divisions have no remainder. We produce the smallest result by dividing by a number that is as large as possible, that is the greatest common divisor. The greatest common divisor can be computed efficiently by Euclid's algorithm, but in this case it was fast enough to simply check all numbers from 1 to 1000 for divisibility.
SETTER'S SOLUTION
Can be found here.
TESTER'S SOLUTION
Can be found here.
XENTASK - Editorial
PROBLEM LINK:
Author:Vaibhav Tulsyan
Testers:Sergey Kulik, Kamil Dębowski
Editorialist:Pawel Kacprzak
DIFFICULTY:
CAKEWALK
PREREQUISITES:
Most basic programming
PROBLEM:
There are $N$ tasks numbered from $1$ to $N$ to perform. Two people, Xenny and Yana, have to complete all these tasks alternatingly. It means that either Xenny performs tasks with odd numbers and Yana with even numbers, or Xenny performs tasks with even numbers and Yana with odd numbers. These two ways are the only possibilities to fulfill the task. Moreover, we know that Xenny takes $X_i$ seconds to complete the $i$-th task, while Yana takes $Y_i$ to do that. The goal of the problem is to compute the minimum possible time in which Xenny and Yana can complete all the tasks.
QUICK EXPLANATION:
Calculate the time needed to complete the tasks for each of two distinct ways of doing that and return the minimum of these times.
EXPLANATION:
The main observation is that since all the tasks have to performed alternatingly by Xenny and Yana, then there are only two distinct ways of completing all the tasks:
- Xenny starts first, which means that Xenny performs tasks with odd numbers: $1, 3, \ldots$, while Yana performs tasks with even numbers: $2, 4, \ldots$
- Yana starts first, which means that Yana performs tasks with odd numbers: $1, 3, \ldots$, while Xenny performs tasks with even numbers: $2, 4, \ldots$
If we assume that for $i = 1, 2, \ldots, N$ $X[i]$ is the time for performing the $i$-th task by Xenny and $Y[i]$ is the time for performing the $i$-th task by Yana, then we can solve the problem by the following pseudocode:
xenny_starts_first = 0 // time to perform all the tasks using the first method,
// i.e. when Xenny starts first
for i = 1 to N:
if i is odd:
xenny_starts_first = xenny_starts_first + X[i] // add time of completing
// the i-th task by Xenny
else:
xenny_starts_first = xenny_starts_first + Y[i] // add time of completing
// the i-th task by Yana
yana_starts_first = 0 // time to perform all the tasks using the second method,
// i.e. when Yana starts first
for i = 1 to N:
if i is odd:
yana_starts_first = yana_starts_first + Y[i] // add time of completing
// the i-th task by Yana
else:
yana_starts_first = yana_starts_first + X[i] // add time of completing
// the i-th task by Xenny
print minimum(xenny_starts_first, yana_starts_first)
Since in order to compute the time of completing the tasks for each of the two methods we need only to iterate once over all tasks, the overall time complexity of this solution is $O(N)$.
AUTHOR'S AND TESTER'S SOLUTIONS:
Setter's solution can be found here.
First tester's solution can be found here.
Second tester's solution can be found here.
Editorialist's solution can be found here.
How to get rid of getting NZEC error while submitting solution in java?
To all who are geting NZEC, try using this format in java:
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException
{
try{
//Your Solve
}catch(Exception e){
return;
}
}
}
Can we see author solutions of codechef contest ?
Can we see author solutions of codechef contest ?
I'm not good in math . Can I become a good coder? if yes how?
I'm not good in math . Can I become a good coder? if yes how?
Hackerearth dfs problem
Can any one explain the logic given in editorial https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/gudi-trapped-in-the-room-july-easy/
How to judge the approach to a DP problem ?
Hi people, I am new to Dynamic programming and I am having trouble in solving such problems. Can someone please make me understand when to apply a 1-D DP approach and when to apply a 2-D DP approach ?
Need Karma Points
Please upvote me. Need karma points to ask questions. Thanks !
PSHTBRTH - Editorial
PROBLEM LINK:
Author:Ivan Fekete
Testers:Sergey Kulik, Kamil Dębowski
Editorialist:Pawel Kacprzak
DIFFICULTY:
Medium
PREREQUISITES:
Game theory, Sprague–Grundy theorem, Segment tree
PROBLEM:
Two players play a game with $N$ binary matrices of size $4 \times 4$ arranged in a row and numbered from $1$ to $N$. They play optimally and make moves in turns. In one move, the current player can select any non-zero matrix, then select any its submatrix containing only $1$'s and replace all of these $1$'s with $0$'s. The player who cannot make a move is declared the loser and the other player is declared the winner. The goal of the problem is to handle $M$ queries. Each query either changes one of the matrices or asks who is the winner if the game is played on the matrices in a range $[L, R]$.
QUICK EXPLANATION:
Use Sprague-Grundy theorem to decompose the game into separated games, each played with a single matrix. Then, for each of $2^{16}$ possible matrices compute the Grundy value of a single game played with that matrix. Finally, handle the queries with a segment tree using the fact that the Grundy value of a game played on matrices in a range $[L, R]$ is a XOR of Grundy values of games played with single matrices in that range.
EXPLANATION:
Subtask 1
In the first subtask, both $N$ and $M$ are at most $10$, so the constraints are quite low. This allows, for example, a solution similar to the one used for the third subtask, but with computing the Grundy values using a plain recursion with any memoization or dynamic programming.
Subtask 2
In the second subtask, we have $N, M \leq 1000$. The solution for these constraints is a slow implementation of the solution for the third subtask. For example, one can use a linear scan in order to handle each of $M$ queries in $O(N)$. Please refer to the next section for more details.
Subtask 3
In this subtask, we have $N, M \leq 10^5$. The intended solution is to use Sprague-Grundy theorem to decompose the game on matrices in a range $[L, R]$ into $R-L+1$ games played with a single matrix, solve each of these games fast, and then compute the winner of the original game using the theorem and results of these smaller games.
Let's consider a game played on a single matrix. We are going to assign every position in such a game a Grundy value, also denoted as mex. The idea is that a terminal position gets value $0$. In our case, the zero matrix, i.e. the matrix containing only zeros, is the terminal position of the game, so it gets value $0$. Then, let's consider any non-zero matrix $A$. Let also $P$ be a set of matrices reachable from $A$ in a single move (remember that in a single move the current player selects a submatrix of $A$ containing all $1's$ and changes all these $1's$ to $0$'s). Then, the Grundy value of matrix $A$ is defined as the smallest non-negative integer which is not a Grundy value of any matrix in $P$. For example, if $A$ have only one cell with value $1$, then $P$ contains only the zero matrix, so the Grundy value of $A$ is $1$ because the Grundy value of zero matrix is $0$. Notice that we can use memoization or dynamic programming in order to compute these Grundy values fast and avoid solving multiple subproblems many times.
Moreover, the position of a game with Grundy value $G$ is a winning position if and only if $G \neq 0$.
There are $2^{16}$ possible matrices to consider, and we are going to compute Grundy values for all of them. For a fixed matrix $A$, this can be done by computing the set $P$ of matrices reachable from $A$ in a single move, computing their Grundy values recursively and then assigning the smallest non-negative integer not present in the set of Grundy values of matrices in $P$ as the Grundy value for $A$. For implementation details please refer to multiple solutions attached below.
Now, we have computed a Grundy value for every possible game played with a single matrix and the next step is to use the Sprague-Grundy theorem in order to get the value of a game played with matrices in a range $[L, R]$. The theorem states that the Grundy value of a game being a composition of $K$ games is the XOR of Grundy values of these games. Thus, if we want to compute the Grundy value of a game played on matrices in a range $[L, R]$, we just XOR Grundy values of games on a single matrix played with matrices in that range. It follows that the first player has a winning position if and only if the Grundy value of the game played with matrices in a range $[L, R]$ is non-zero.
Finally, to handle all the $M$ queries fast enough, we can use a segment tree or a Fenwick tree, in order to support both computing XOR of a range of values and updating a single value in that range. Segment tree supports these operations in $O(\log N)$ time, so after the precomputation of Grundy values is done, the complexity of handling all the queries is $O(M \cdot \log N)$. For implementation details please refer to multiple solutions liked below.
AUTHOR'S AND TESTER'S SOLUTIONS:
Setter's solution can be found here.
First tester's solution can be found here.
Second tester's solution can be found here.
Editorialist's solution can be found here.
COPR16G: Editorial
PROBLEM LINK:
Author:Bhuvnesh Jain
Tester:Bhuvnesh Jain
Editorialist:Bhuvnesh Jain
DIFFICULTY:
EASY
PREREQUISITES:
Diophantine Equations, GCD
PROBLEM:
You are provided with coins of denominations a and b.
You are required to find the least value of n, such that all currency values greater than or equal to n can be made using any number of coins of denomination a and b and in any order. If no such number exists, print -1 in that case.
EXPLANATION:
Let us first write the statement as mathematical equations and then procede furthur.
ax + by = n, where x and y are integers.
and au + bv = n+1, where u and v are integers.
The above equations hold for all integers >= n, if solution exists. Let us subtract the 2 equations. We get,
ar + bs = 1, where r and s are integers.
Hence we can easily see that solutions exists iff gcd(a, b)==1. (Using the condition for solvability of linear Diophantine equation).
Now we need to find the value of least possible n. We can also see the above problem as finding the largest interger which can be expressed as ax + by for some integers, x and y. A simple google search could have helped you, hence the name "GET AC IN ONE GO". The answer is simply (a.b - a - b). You can see a detailed proof here.
Hence, the solution is (a.b - a - b + 1), when gcd(a, b) = 1 else -1.
COMPLEXITY
O(log(max(a, b))) per test case
AUTHOR'S SOLUTION:
Author's solution can be found here.
COPR16A : Editorial
PROBLEM LINK:
Author:Bhuvnesh Jain
Tester:Bhuvnesh Jain
Editorialist:Bhuvnesh Jain
DIFFICULTY:
EASY
PREREQUISITES:
Matrix Exponentiation, Modular Multiplication
PROBLEM:
Calculate the number of times fibonacci(k) is called in the recursive function given for fibonacci sequence.
EXPLANATION:
The solution is basically $(n-k+1)^{th}$ Fibonacci number for k>=1.
The above can be proved by induction.
Proof By Induction (we will prove here for $k \geq 1$)
For n=0, there are no value of $k$ possible, so the proposition is trivially true.
For n=1, we can only pick k=1. It is true that fibonacci(1) appears one time i.e. $F_{1−1+1} = F_{1} = 1$
Now we assume that our proposition is valid for all numbers smaller than n, (strong induction) and we’ll prove it for n. Let $1 \leq k \leq n$. Let us draw the recursion tree for $F_{n}$, which we call $T_{n}$. If $k = n$, then clearly $F_{n}$ only shows up 1 time in $T_{n}$ i.e at the root of the recusion tree, so the result holds. Likewise, if $k = (n−1)$, the only place we see $F_{n−1}$ is as the root of our left subtree, the larger one. So the result holds in that case as well, since $F_{n−(n−1)+1} = F_{2} = 1$.
Let us then assume that $k \leq (n-2)$, which allows us to use the inductive hypothesis on the left and right subtrees of $T_{n}$.
By induction, the number of times $F_{k}$ appears in the left subtree is $F_{n−1−k+1} = F_{n−k}$, and the number of times it appears in the right subtree is $F_{n−2−k+1} = F_{n−k−1}$. Thus the number of times it appears in $T_{n}$ is the sum of values at both the sub-trees, i.e. $F_{n−k} \ + \ F_{n−k−1} = F_{n−k+1}$, which was to be shown.
Hence By Principle of mathematical induction the result holds all n and $k \geq 1$.
But for k=0 the answer is $(n-1)^{th}$ Fibonacci number. The corner case of k=0 can be seen from the recursion tree ${T}_{n}$ drawn showing that it occurs the same number of time as k=2. So the formula is (n-2+1) = (n-1) as stated above.
To find the $n^{th}$ fibonacci number, we can use Modular exponentiation which gives a complexity of O(log n).
But there is a more faster way to do this. You can refer to Codeforces blog for this. My solution uses the same concept but in a bit different and faster way.
Also, the test cases were weak as most of the solutions should have failed. As per the constraints, the modulo should be taken m where 1<=m<=$10^{12}$. As ($10^{12}$. $10^{12}$) will overflow in C++, Java etc. without using big integer libraries, we needed to use Modular multiplication. One way to do this is using the same concept as modular multiplication and replacing the multipplication operations by additions. Below is the C++ function to do the same.
long long mulmod(long long a,long long b,long long c)
{
long long x = 0,y=a%c;
while(b '>' 0)
{
if(b%2 == 1)
{
x = (x+y)%c;
}
y = (y*2)%c;
b /= 2;
}
return x%c;
}
You can also use a O(1) hack for the above as given in Praveen Dhinwa's blog.
COMPLEXITY
O(${log}^2$n) or O(logn), depending on implementation of Modular multiplication function.
AUTHOR'S SOLUTION:
Author's solution can be found here.
Invitation to Mathemagic 2017- 24 Hour contest
Love solving Mathematical problems. Then get ready for some action ahead.
Computer Science Association, BITS PILANI, Pilani Campus presents you their flagship contest, "Mathemagic 2017". It is being organised as a part of our technical fest, Apogee.
Contest Link :https://www.hackerrank.com/mathemagic-bits
Start Time :17:00 IST, 23rd March' 2017
Prizes:
First Place: INR 3000, Second Place: INR 2000, Third Place: INR 1000.
The contest consists of 10 problems of varying difficulty similar to ones available at Project Euler and ad Infinitum contests. The problems have been set such that even beginners can attempt a few problems and even the best coders find tough job ahead at the hard ones. Although Mathematical insights and tricks would help you find the logic in the problem, programming will be your way out for the full solution. There will be 4 "TEXT" based problems where the input is provided in the question, similar to the format in Project Euler. Rest 6 problems will be programming based. The editorials of all the problems with the Setter's code will be uploaded immediately after the contest so that you may benefit from it.
The problems have been set by me. I would like to thank Morass for testing the problems and providing valuable feedback as well. I would also like to thank Chirag Agarwal and CSA juniors for their feedback as well.
To register for the contest, you just need a Hackerrank handle and then just click on the link mentioned above for registering for the contest.
I hope you enjoy the contest and Happy Coding :)
COD32017 CODA2017 Getting WA
Need to know on which inputs does my solution fails.
Q1:https://www.codechef.com/PRCD1701/problems/COD32017/
my solution: https://www.codechef.com/viewsolution/13122779
Q2:https://www.codechef.com/PRCD1701/problems/CODA2017/
my solution: https://www.codechef.com/viewsolution/13117798
Please suggest list of concepts must for cp?
like hashing,dp etc learning links please.. ps-im beginner
Coding Calender:Great App Suggestion
Here is the link-Check out this app: Coder's Calendar
https://play.google.com/store/apps/details?id=com.corphots.coderscalendar
Never miss any contest!...
features: 1-you can set reminder
2-all ccontests from codechef,hacker-rank,hacker-earth,codeforces,etc
3-simple gui
4-u can share contest link
and many more
SPOJ GSS 1 Wrong Answer
That is my code for SPOJ problem GSS1, find the problem here
The code gives WA, kindly tell me why.
Any help would be appreciated.
Drop in Ratings after lunchtime or cookoffs
Why does my rating drop by a few points after participating in a cookoff or lunchtime,even after solving one question.
I participated in February long challenge,got 420 points and my overall rating bumped to 1775. After participating in Lunchtime 2017,and solving the first question, it fell by 82 points.
Why?
PRCN16B - Editorial
PROBLEM LINK:
Editorialist:Rounaq Jhunjhunu Wala
DIFFICULTY:
EASY
PREREQUISITES:
Modulo
PROBLEM:
Given a number $N$, we need to find the sum of all integers $i$ from $1$ to $N$ such that $2^i+1$ is divisible by $3$.
EXPLANATION:
We can try the naive approach of finding $2^i+1$ for $1 \leq i \leq N$, and check for 3-divisibility. To handle large exponents, we can take mod at every stage of exponentiation so that the resulting number is small. Finally we can use fast exponentiation to find the powers of 2. But, we can easily see that all the above approaches are $\Omega(N)$ and hence won't work on the original constraints.
We would need a formula for solving this problem, which we will derive now. We know that $2^i+1 \equiv 0 \mod 3$, which is equivalent to $2^i = 2 \mod 3$. Now, $2 \equiv 2 \mod 3$ and $2^2 \equiv 1 \mod 3$, and by the property (a*b)%m = ((a%m)*(b%m))%m
, we can deduce that $2^i \equiv 2 \mod 3$ if $i$ is odd, else $2^i \equiv 1 \mod 3$.
THe above reduces the problem to just finding the sum of odd integers from $1$ to $N$, which can be easily proved to be equal to $\lfloor \frac{N+1}{2} \rfloor ^ 2$.