< Back to forum

SIGSEGV runtime error in dp implementation

why am I getting SIGSEGV  again and again...???...some1 plz help me out....

problem link  -->  https://www.spoj.com/problems/COINS/

submission code ::

#include<bits/stdc++.h>
using namespace std;
long long int fun(long long int );
long long int has[100000];
int main()
{
    long long int a;
    while(scanf("%lld",&a)!=EOF)
    {
        long long int r;
        r=fun(a);
        printf("%lld\n",r);
    }
    return 0;
}
long long int fun(long long int n)
{
    long long int i;for(i=0;i<=12;i++){has[i]=i;}
    for(i=13;i<100000;i++)
    {has[n]=has[n/2]+has[n/3]+has[n/4];}
    if(n<100000)
    {
        return(has[n]);
    }
    else
    {
        return(fun(n/2)+fun(n/3)+fun(n/3));
    }
}

 

Asked by: ABHISHEK_KUMAR2 on April 7, 2019, 1:04 p.m. Last updated on April 7, 2019, 1:04 p.m.


Enter your answer details below:


Preview

Enter your comment details below:

Preview




2 Answer(s)

avatar

You have a lots of mistakes in your code. Try to fix them.

note 1 : in has[n] = has[n/2] + has[n/3] + has[n/4] replace n with i.

note 2 : in return statement fun(n/3) appear two times.

rishup132 last updated on April 7, 2019, 1:04 p.m. 0    Reply    Upvote   

avatar

The problem can be done using normal recursion only. :)    

Samrat_De last updated on April 7, 2019, 1:04 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.