You are not logged in. Please login at www.codechef.com to post your questions!

×

Spoj AMR11E code memoization technique trouble

The question is http://www.spoj.com/problems/AMR11E/

My problem is why does the input 1 2 gives output zero?

My code is:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;


int a[1000]={-1};
int prime[1500];

int lucky(int);
void primes();

int main()
{

    int t,n;
    scanf("%d",&t);
    primes();

    while(t--)
    {
        scanf("%d",&n);
        a[0]=30;
        printf("%d\n",lucky(n));
    }

    return 0;
}

int lucky(int n)
{

    int i,j,count,r;
    if(a[n-1]!=-1)
    return a[n-1];
    else
    {   
        if(a[n-2]==-1)
        r=lucky(n-1);
        for(i=a[n-2]+1;i<2665;i++)
        {   
            count=0;
            for(j=0;prime[j]<sqrt(n);j++)
            {
                if(i%prime[j]==0)
                {
                    count++;
                    if(count>=3)
                    break;
                }
            }
            if(count>=3)
            break;
        }

        a[n-1]=i;
        return i;
    }
}

void primes()
{   
        int i,j,flag,k=0;;
        for(i=1;i<=2665;i++)
              {

                    flag=1;
                    for(j=2;j<=sqrt(i);j++)
                    {
                                if(i%j==0)
                                {         
                                flag=0;
                                break;     
                                }
                    }
                    if(flag==1)
                    {
                        prime[k]=i;
                        k++;
                    }
              }
}

My technique is to first store the primes in an array, then divide the numbers to get the answer. It would be of great help if anyone can tell me where is the error in my code?

asked 25 Nov '14, 15:40

az1az14_3034's gravatar image

1★az1az14_3034
12
accept rate: 0%

edited 25 Nov '14, 15:47

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,911
×1,136
×107

question asked: 25 Nov '14, 15:40

question was seen: 2,046 times

last updated: 25 Nov '14, 15:47