-4
import java.util.Scanner;
public class theatre_square
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
        theatre_square ob = new theatre_square();
        long x = ob.cal(m,s), y = ob.cal(n,s);
        System.out.println(x*y);
    }
    long cal(long a, long b)
    {
        if(a<=0)
            return(0);
        else
            return (1+cal(a-b,b));
    }
}

Input: 1000000000 1000000000 1

Error:

Exception in thread "main" java.lang.StackOverflowError at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17) at theatre_square.cal(theatre_square.java:17)....... and so on.

I think it has to do something with the return statement inside in the recursion function.

khelwood
  • 55,782
  • 14
  • 81
  • 108
vishal_x
  • 3
  • 1
  • What are you trying to calculate? – khelwood Sep 20 '20 at 11:54
  • 2
    You are calling your mathod `cal` with `b == 1`. This means, you build up a recursive stack with 1 billion (!) recursive method calls. This simply might be too big for a usual stack. – Seelenvirtuose Sep 20 '20 at 11:56
  • @khelwood Just solving some questions. Here's the link https://codeforces.com/problemset/problem/1/A – vishal_x Sep 20 '20 at 12:02
  • @Seelenvirtuose Any work around for it ?? – vishal_x Sep 20 '20 at 12:02
  • @VishalPatel Have tried it. Doesn't work. – vishal_x Sep 20 '20 at 12:06
  • 1
    @Seelenvirtuose is right. you are calling recursive function 1000000000 times. – Vishal Patel Sep 20 '20 at 12:09
  • 3
    The answer is: you have to understand what you are doing. Every method invocation allocates memory on the stack, that stays in used until the method returns. Too many nested method calls and you run out of memory. So what is at fault here is your ASSUMPTION that your code should just work. – GhostCat Sep 20 '20 at 12:10
  • 1
    @GhostCat Okay. I will just use for loop then. Thanks for your help kind stranger. – vishal_x Sep 20 '20 at 12:15
  • Your code and output should be linked. Your output is from a different version of the code. Also, if b is negative, then eventually a will overload a long. – NomadMaker Sep 20 '20 at 13:35
  • Hi and welcome to SO. Please read [How to ask](https://stackoverflow.com/help/how-to-ask) then try to improve your question by editing it, focusing on a specific problem and giving details about it (code, configuration, error messages, exact problem your are facing. – Sourav Sep 21 '20 at 02:46
  • 1
    Does this answer your question? [What is a StackOverflowError?](https://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror) – corashina Sep 21 '20 at 13:29

1 Answers1

0

i think you can solve the problem without recursion and for loop.

import java.util.Scanner;
public class theatre_square
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        long m = in.nextLong(), n = in.nextLong(), s = in.nextLong();
        theatre_square ob = new theatre_square();
        long x = ob.cal(m,s), y = ob.cal(n,s);
        System.out.println(x*y);
    }
    
    
    long cal(long m, long a)    {
        long count = m / a;
        long rem = m % a;
        if(rem  > 0){
            count++;
        }
        return count;
    }
}
Vishal Patel
  • 554
  • 6
  • 15