 # CHEFLR - Editorial

#1

Problem link : contest practice

Difficulty : Simple

Pre-requisites : basic math

Solution :

The problem is one of the easiest in the set. The solution is as follows.

When we have an odd-length string S, we can make one turn and set some flag the the answer is even. Otherwise, we should keep in mind than the answer is odd.

The remaining part always has even length, and we can threat the tree as the octary one and numerate the nodes by consecutive natural numbers. When we have found the node number in this tree, we will only need to multiply it by two and possibly to subtract one. The tree is octary, so the consecutive symbols can be grouped in pairs and each pair will denote a single edge of the tree. Namely, in the order from left to right the order of pairs is “ll”, “lr”, “rl”, “rr”.

So we just maintain the variable index and each time we traverse one edge of our tree, we multiply it by 4, and then add -2, -1, 0 or 1, depending on the type of the edge: -2 for “ll”, -1 for “lr” and so on.

At the end we just need to find the index-th even (or odd) number. The parity depends on the parity of the length of the string that denotes the path.

Setter’s solution: link

Tester’s solution: link

#2

Or we could do something like the following -

For each level, keep a counter whether the level number is odd or even.

We have ans = root = 1

Now traverse through the string.

``````If the next character is 'l' and we are on an even numbered level -

ans = ans x 2

else if the next character is 'l' and we are on an odd numbered level -

ans = ans x 2 - 1

else if the next character is 'r' and we are on an even numbered level -

ans = ans x 2 + 2

else if the next character is 'r' and we are on an add numbered level -

ans = ans x 2 + 1
``````

Finally,

``print ans``

#3

`char a` size should be 100000+1. This extra one byte is to hold ‘\0’ value.

#4

Can anyone tell me for which input my code is not working
#include
#include<stdio.h>
#include<string.h>
char s;

unsigned long long int flag=1,ans,x,a;

using namespace std;

int main()

{

unsigned long long int t,i,p=1,n=0,k=0,z=0;

unsigned long long int flag,ans=1000000007,x;

cin>>t;

for(i=0;i<t;i++)

{
flag=1;
p=1,n=0,k=0,z=0;

``````cin>>s;

a = strlen(s);

while(a)

{

n = (p % 2) - 1;

k = 2 + n;
if(s[z] == 'l')
{
flag = flag*2 + n;

}
else if(s[z] == 'r')

flag = flag*2 + k;

p++;
z++;
a--;
}
``````

flag = flag % ans;

printf("%lld
",flag);

}

}

#5

Can someone help me understand why my code, here, has failed? I think it’s OK unless it’s a fence post ever that I’m not able to figure out.

#6

Can someone please tell me what is wrong with my code found here
http://www.codechef.com/viewsolution/4728506

#7

Can anyone please figure it out why the enormous input method is unable to fetch the string from buffer ?
Please!!
//Chef Left-Right

``````#include<stdio.h>
#include<string.h>

#define BUFFER 1000000

int main()
{
int test, i ,value=1, length,n,j=0;
int limit=1000000007;

char array[BUFFER+1];

scanf("%d",&test);

while((n = fread(array, 1, BUFFER, stdin)))
{
if(test == 0)
break;

for(i=0; i<n; i++)
{
if(array* != '
``````

')
{
value=value%limit;

``````         if(j%2==0)
{
if (array*=='l')
{
value=((value%limit)*2)%limit;
}
else
{
value=(((value%limit)*2)%limit)+2;
}
}

else
{
if(array*=='l')
{
value=(((value%limit)*2)%limit)-1;
}
else
{
value=(((value%limit)*2)%limit)+1;
}
}

j++;
}

else
{
printf("%d
``````

",value%limit);
j=0;
value=1;
}
}
}

``````return 0;
}``````

#8

Please help, getting WA!What’s wrong with this python code?

http://www.codechef.com/viewsolution/4862976

Pls help.

#9

I dont understand the editorial explanation, can someone elaborate in simple terms ?

#10

@rok0707

hi…

here is your corrected solution your corrected solution (AC)

here I want to mention some points which you may havn’t taken care

POINT 1

• first one when you are taking mod some even level value may be convert into odd value because there are so many times mode happen that’s why it is possible

Solution 1

• you should a separate variable for keeping track of level or you can use index also

POINT 2

• second one may be your root* is exact multiple or equal to mod than root*%mod=0 later (may be )2root-1 become negative

Solution 2

• always check if(root*>=mod) root*%=mod

• root*=(root*+mod)%mod;

hope it help you…

happy coding

#11

Can anyone let me know whats wrong with my solution. I have done the same but getting WA.

https://www.codechef.com/viewsolution/18744849

Thanks in advance

#12

This wasn’t working. TLE error.

#13

This worked for me. Are you sure nothing else was wrong with your code?

#14

It will work. It worked for me too. And most probably 90% of people have tackled this problem this way. @chefkaushik94 : Optimize your code.

#15

I did the same. But it shows WA. Can someone please tell my mistake?
http://www.codechef.com/viewsolution/4807475

#16

Please someone let me know what’s wrong with my code http://www.codechef.com/viewsolution/4811858 I followed the same method, and it worked for provided test cases, but on submission, it keeps showing wrong answer.

#17

@rok0707 modulo 10^9+7.

#18

@rok0707

hi…

here is your corrected solution
https://www.codechef.com/viewsolution/7887676

here I want to mention something which you may havn’t taken care
first one when you are taking mod some even level value may be convert into odd value because there are so many times mode happen that’s why you should a separate variable for keeping track of level or you can use index also

second one may be your root* is exact multiple or equal to mod than root*%mod=0 later (may be )2root-1 become negative

hope it help you…

happy coding