You are not logged in. Please login at www.codechef.com to post your questions!

×

Why does this code throw NZEC?

I've been racking my brain over this for hours and I can't figure out why I keep getting NZEC when submitting to CodeChef. I am generally good with these problems, but this one escapes me. The code runs fine locally:

package CodeChef.LeftRightTree;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
// @link: http://www.codechef.com/problems/CHEFLR
public class Main {
    public static void main(String args[]) throws Exception {
        int numTurns;
        int currentLine = 0;
        String line;
        char direction;
        boolean isEven;
        BigInteger currentNode;
        BigInteger mod = new BigInteger("1000000007");

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        line = buffer.readLine();
        numTurns = Integer.parseInt(line);

        while (currentLine++ < numTurns) {
            currentNode = new BigInteger("1");

            line = buffer.readLine();

            for (int i = 0; i < line.length(); i++) {
                direction = line.charAt(i);
                isEven = currentNode.mod(new BigInteger("2")).equals(BigInteger.ZERO);

                currentNode = currentNode.multiply(new BigInteger("2"));

                if(isEven){ // node ID is even
                    currentNode = currentNode.add(new BigInteger("-1"));
                }

                if(direction == 'r'){
                    currentNode = currentNode.add(new BigInteger("2"));
                }
            }

            out.println(currentNode.mod(mod));
        }

        out.flush();
    }
}

asked 27 Nov '14, 08:18

ghurtado's gravatar image

0★ghurtado
1
accept rate: 0%


Refer to this link HERE

link
This answer is marked "community wiki".

answered 27 Nov '14, 19:48

namankumar's gravatar image

0★namankumar
2521624
accept rate: 0%

Sorry, but that's not helpful at all. The answer you linked to only gives the definition of NZEC, which is very basic and I already knew.

(28 Nov '14, 10:46) ghurtado0★

And you tried your code with 5 (test cases) x 105 digits long string with all 'l' (L characters only)?

Tip: write a program to generate such input file and the use it for testing ;-)

link

answered 27 Nov '14, 16:16

betlista's gravatar image

3★betlista ♦♦
16.9k49115225
accept rate: 11%

edited 27 Nov '14, 16:17

In the end the issue was embarrassingly simple: I had left in the package declaration.

package CodeChef.LeftRightTree;

After removing it, my program runs.

Unfortunately, it looks like I can't accept my own answer, but this should definitely be part of the NZEC FAQ (if one exists).

link
This answer is marked "community wiki".

answered 28 Nov '14, 10:44

ghurtado's gravatar image

0★ghurtado
1
accept rate: 0%

edited 28 Nov '14, 10:44

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,302
×422

question asked: 27 Nov '14, 08:18

question was seen: 873 times

last updated: 28 Nov '14, 10:46