someone please tell me whats wrong with this code..its for problem CHEFLR

import java.io.*;

class Coding{
private static final long mod=1000000007;
public static void main(String[] args) throws IOException
{
int n,k=0;
long total,val;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
for(int j=0;j<test;j++)
{
String tre = br.readLine();
long len=tre.length();
long x=1;
for(int i=0;i<tre.length();i++)
{
if(tre.charAt(i)=='l')
{
x=((x-1)*2+1)%mod;
}
else
{
x=((x-1)*2+2)%mod;
}
}
if((len+1)%2==0)
{
total=(long) (((Math.pow(2, len+1)-1)/6))%mod;
val=((x+total)*2)%mod;
}
else
{
total=(long) ((((Math.pow(2, len+1)-2)/6)))%mod;
val=((x+total)*2-1)%mod;
}
System.out.println(val);
}
}
}

Problem is result of Math.pow and very large len (length of S).
Check this code:

long len = 100000;
long mod = 1000000007;
System.out.println(Math.pow(2, len + 1));
System.out.println((long) ((((Math.pow(2, len + 1) - 2) / 6))) % mod);

As you can see, Math.pow results in infinity although further operations will “narrow” it to long.
You should avoid such big operations on big input numbers and always think, whether the operation (+, *, pow,…) will not exceed primitive type limits.
You can check my solution

Btw, I have different output for this string:

llrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrlllrllrlrrrllrlrlrlrl

so you can debug :slight_smile:

[Yours: 693717252, Mine: 111003291]

Test Cases for CHEFLR

import random
from sys import stdout

array = ''
t = random.randrange(1, 10)

#stdout.write(str(t)+"\n")
f=open("CHELFR_TEST.html","w")
#print t
f.write(str(t)+"\n")
while t>0:
    ran = random.randrange(1, 10**5)
    array=''
    while ran:
        array = array + random.choice("l""r")
        ran-=1
    #stdout.write(array+"\n")
    #print array
    f.write(array+"\n")
    t-=1
f.close()

Here is a test case generator for CHEFLR in python 2.7.

Run this cases on a successfully compiled program and compare the outputs.