Recaman’s Sequence

Time Limit: 3000MS Memory Limit: 60000K

Total Submissions: 22392 Accepted: 9614

Description

The Recaman’s sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence, otherwise am = am−1 + m.

The first few numbers in the Recaman’s Sequence is 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 …

Given k, your task is to calculate ak.

Input

The input consists of several test cases. Each line of the input contains an integer k where 0 <= k <= 500000.

The last line contains an integer −1, which should not be processed.

Output

For each k given in the input, print one line containing ak to the output.

Sample Input

7

10000

-1

Sample Output

20

18658

#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h> using namespace std;
#define MAX 500000
long long int a[MAX+5];
bool tag[10000000];
void fun()
{
a[0]=0;
tag[0]=1;
for(int i=1;i<=MAX;i++)
{
a[i]=a[i-1]-i;
if(a[i]<0||tag[a[i]]==1)
a[i]=a[i-1]+i;
tag[a[i]]=1;
}
}
int main()
{
int n;
memset(tag,0,sizeof(tag));
fun();
while(scanf("%d",&n)!=EOF)
{
if(n==-1)
break;
printf("%d\n",a[n]); }
}

POJ-2081 Recaman's Sequence的更多相关文章

  1. POJ 2081 Recaman's Sequence

    Recaman's Sequence Time Limit: 3000ms Memory Limit: 60000KB This problem will be judged on PKU. Orig ...

  2. Poj 2081 Recaman's Sequence之解题报告

                                                                                                         ...

  3. poj 2081 Recaman's Sequence (dp)

    Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 22566   Accepted: 96 ...

  4. poj 2081 Recaman&#39;s Sequence

    開始还以为暴力做不出来,须要找规律,找了半天找不出来.原来直接暴力.. 代码例如以下: #include<stdio.h> int a[1000050]; int b[100000000] ...

  5. POJ 2081 Recaman&#39;s Sequence(水的问题)

    [简要题意]:这个主题是很短的叙述性说明.挺easy. 不重复. [分析]:只需要加一个判断这个数是否可以是一个数组,这个数组的范围. // 3388K 0Ms #include<iostrea ...

  6. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  7. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  8. POJ 3017 Cut the Sequence

    [题目链接] $O(n^2)$ 效率的 dp 递推式:${ dp }_{ i }=min\left( dp_{ j }+\overset { i }{ \underset { x=j+1 }{ max ...

  9. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

随机推荐

  1. [AX]AX2012 R2 采购订单的“Request change”

    在采购订单List或者Detail窗口的操作面板上有一个叫做“Request change”的按钮,如果这个按钮是激活的,采购订单不能直接编辑,而必须先使用这个按钮请求修改后,采购订单才能进入编辑状态 ...

  2. Docker-创建和分享应用(3)

          Dockerfile定义容器内环境中发生的事情.对网络接口和磁盘驱动器等资源的访问在此环境中进行虚拟化,该环境与系统的其他部分隔离,因此您需要将端口映射到外部世界,并具体说明要“复制”哪些 ...

  3. form提交表单没接收到$_POST

    分享一个最近做项目遇到的奇葩经历: 很奇怪的,我在弄一个表单提交的时候,后台验证就报了非post提交错误 我就郁闷了,我form明明写的method为post,不可能是非post错误啊 经历反应测试, ...

  4. linux 下的爆破工具hydra

    http://www.cnblogs.com/mchina/archive/2013/01/01/2840815.html 安装手册 http://www.aldeid.com/wiki/Thc-hy ...

  5. 在recycler中写的布局不起作用

    把 LinearLayout 改成 RelativeLayout   ok了 创建的两种方式 1.LayoutInflater.from(parent.getContext()).inflate(R. ...

  6. centos6.4搭建ftp服务器

    在centos环境下搭建ftp服务器,选择用vsftpd. 1.检测是否已经安装vsftpd # rpm -qa | grep vsftpd 如果已经安装vsftpd,会显示相应版本号.没有任何提示, ...

  7. React Native(三)——推送jpush-react-native

    瞬间,有种满血复活的赶脚…… 原因呢,就是熟悉了rn项目的套路:当老大问道,“推送功能看了还是没看呢?”的时候,虽然一直没有调试通,但还是不怯场的回答,“看了,按照网上说的也配了,但是还是用不了,不知 ...

  8. 扩展方法以及LinQ的学习

    我们今天学习的内容其实还是蛮多的,学习了自动属性,扩展方法,复习初始化器,以及LinQ的一些方法,其实感觉有些还是很熟悉的,在前面的学习过程中有时间感觉学习的知识知道了怎么使用,但是还没有在项目中使用 ...

  9. opencv3——ANN算法的使用

    最近刚转用opencv3,使用ANN算法时遇到了一些问题,记录下来. 训练神经网络的代码如下: //创建ANN Ptr<ANN_MLP> bp = ANN_MLP::create(); 设 ...

  10. Python在mysql中进行操作是十分容易和简洁的

    首先声明一下,我用的是Windows系统! 1.在Python中对mysql数据库进行操作首先要导入pymysql模块,默认情况下,Python中是没有安装这个模块的, 可以在Windows的命令行中 ...