Question

I have doubt..its running fine on ideone and my gcc comiler but..codechef is giving runtime error..p please help

        #include<iostream>
        #include<cstdio>

        using namespace std;
       int main()
       {        
       //printf("thisis it");
            int i,n,t;
     long int a[200000],f[100008];

        f[0]=0;f[1]=1;
            for(i=1;i<200000;i++)
             a[i]=0;

             i=1;
                while(f[i]<=100000)
                   {
                    i++;
                f[i]=f[i-1]+f[i-2];
            a[f[i]]=1;
                   }
                scanf("%d",&t);
                while(t--)
                {
            scanf("%d",&n);
            if(a[n]==1)
            printf("Yes\n");
            else
                printf("No\n");
               }
    return 0;
               }

Please help...why its giving run time error on codechef...while everything is fine....

Was it helpful?

Solution

I suppose an error is here:

a[f[i]]=1;

even 40th fibonacci number (102334155) will be greater than 200000, so it will be out of a's range.

Update: sorry, I haven't noticed that the loop runs until f[i] <= 10000, not i <= 10000.

Another possible problem here is

if(a[n]==1)

you don't check n to be less than a's size, if someone would input a very big number, the code can produce a segfault.

OTHER TIPS

First, why are you initing a[] only from the second value? a[0] should be un-inited?

Second, it seems like your problem is with a[f[i]]=1; your addressing the value of the f[i] the 28th value should be 317811 which exceeds a size.

Something over here is over complicated in this code, I would recommend revising it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top