poj 2081 Recaman's Sequence
#include <iostream>
using namespace std;
int list[500001];
bool flag[10000000];
int main()
{
int n,top=0;
fill(flag,flag+sizeof(flag),1);
list[0]=0;flag[0]=0;
while(scanf("%d",&n)&&n!=-1)
{
while(top<n)
{
if(list[top]-top-1>0&&flag[list[top]-top-1])
{
top++;
list[top]=list[top-1]-top;
}
else
{
top++;
list[top]=list[top-1]+top;
}
flag[list[top]]=0;
}
printf("%d\n",list[n]);
}
return 0;
}