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. go fmt格式化----“占位符”

    https://studygolang.com/articles/2644 https://studygolang.com/static/pkgdoc/pkg/fmt.htm

  2. 禁用滚动视图ListView、ViewPager、ScrollView、HorizontalScrollView、WebView边界颜色渐变

    禁用滚动视图ListView.ViewPager.ScrollView.HorizontalScrollView.WebView边界颜色渐变 ListView.ViewPager.ScrollView ...

  3. 搭建项目Maven+springMVC+hibernate时,JUnit測试出现报ClassNotFoundException错误的解决

    近期在搭建Maven+springMVC+hibernate项目,正常启动项目时一切正常.但JUNIT測试时出现报ClassNotFoundException错误,经过细致排查发现没有生成class文 ...

  4. nodejs服务器部署教程三

    安装mongodb数据库 如何在ubuntu上安装mongodb数据库,其实官方文档写的很清楚啦 sudo apt-key adv --keyserver hkp://keyserver.ubuntu ...

  5. 品鉴同事发来的炸金花的PHP程序代码

    今天同事发来了一个炸金花的PHP程序,这个代码实现了两个人通过各自的三张牌进行权重计算,得到分数进行比较得到谁的牌大,我觉得里面还有一些问题,代码如下: <?php /** 每张牌的分值为一个2 ...

  6. CallByValue和CallByName区别

    /** * Created by root * Description :CallByValue:进入函数就得先计算实参的值:CallByName:函数体重使用到的时候才计算 */ object Ca ...

  7. SpringBoot(十三)-- 不同环境下读取不同配置

    一.场景: 在开发过程中 会使用 开发的一套数据库,测试的时候 又会使用测试的数据库,生产环境中 又会切换到生产环境中.常用的方式是 注释掉一些配置,然后释放一下配置.SpringBoot提供了在不同 ...

  8. Unity透明Shader

    Shader "Custom/Blocks" { Properties { _Color (,,,) _MainTex ("Albedo (RGB)", 2D) ...

  9. BugHD 与你的应用一起成长

    BugHD 新增功能 1.新增安装量.启动量的统计功能 BugHD SDK 1.3.0新增安装量.启动量的统计功能,在崩溃分析页面右上角可看到安装量和启动量. BugHD 体验优化 1.Android ...

  10. 《JavaScript 秘密花园》

    恰巧今天是传统民间重要的节日之一--七夕节: 被大家挂在嘴上最多的一句话便是:有对象了吗?这不-- 这样的话,那咱就先给new出一个对象吧: var boyfriend = new Object(); ...