Clarification required in SEP14 CHEFLR Problem

SEP14 CHEFLR Problem is available in this link.

To simplify this problem, assume the modulo value is 7, then what is resultant node number for the path rll and rlll. I understand that it should be 0 and 6.
But the successful code(CodeChef: Practical coding for everyone) giving it as 0 and -1. please correct me if i am wrong.

#include <stdio.h>

int main ()
{
  char ch=0;
  int v=0;
  int i =1, t_l =0, t_r=0, tc=0, c=0, ret=0 ,count= 0, s =0;
	int  mod= 1000000007;


  scanf("%d\n", &tc);

  while (tc){
    --tc;
    i =1; c=1; v=1;
    ret=0;
	s =0;
	++count;
    while (i)
    {
        ret = scanf ("%c", &ch);

        if(ret != 1){
         ch='q';
        }
		++s;
        c=!c;
 
        if ( 0== c){
          t_l=0;t_r=2;
        }
        else{
          t_l=1; t_r=1;
         }

        switch(ch)
        {
          case 'r':
          {

            v=(v*2) + t_r;

				v = v%mod;
           	if ( 0 == v)
            {
              v = mod;
            }

          }
          break;

          case 'l':
          {
           	 v=(v*2) - t_l;

            v = v%mod;
           	if ( 0 == v)
            {
             v = mod;
            }

          }
          break;

          default:
          { 
 
    	      v=v%mod;
             printf ("%d\n", v);
	          i =0;
          }
           break;
        }
    }
  }

  return 0;
}

Yeah, you are right.In C/C++ (negative value)%mod will result a negative value.i.e;Upto rll, the answer is 0,considering the next ā€˜lā€™ what you are doing is (2*ans)-1.As ans=0, the final answer is 0-1=-1. And you are printing -1%7=-1(in C/C++).What you need to do is you have to convert the negative value to respective positive.And this can be done using this technique: (ans+mod)%mod.So the final answer is (-1+7)%7=6.This error will not occur if mod=10^9+7.

1 Like

Thanks. In that case, my code also should work.But I could not submit this, due wrong answer. Can you please help me to find the mistake ?