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. SqlServer当前月份时间

    SqlServer当前月份时间 SELECT -DAY(getdate()+-DAY(getdate()))

  2. WPF布局管理之Canvas、InkCanvas (转)

    一.Canvas 在WPF中子元素的绝对定位的布局控件 其子元素使用Width.Height定义元素的宽度和高度 使用Convas.Left(Convas.Right).Convas.Top(Conv ...

  3. ios开发之--AVAudioPlayer/AVPlayer的应用

    项目当中用到了音频播放器,所以就参考官方文档,写了一个,代码如下: .h #import <UIKit/UIKit.h> @interface hAudioPlayViewControll ...

  4. C 修改命令行文本颜色

    #include <Windows.h> #include <stdio.h> int main() { HANDLE h = GetStdHandle(STD_OUTPUT_ ...

  5. 【安全开发】java安全编码规范

    申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...

  6. Burp Post、Get数据包转为上传multipart/form-data格式数据包

    方法一: 新建一个网页进行上传,代码代码如下: <html> <head></head> <body> <form method="po ...

  7. 使用Xcode自带的单元测试

    今年苹果推出的iOS8和Swift的新功能让人兴奋.同时,苹果对于Xcode的测试工具的改进却也会影响深远.现在我们来看下XCTest,Xcode内置的测试框架.以及,Xcode6新增的XCTestE ...

  8. Splash autoload() 方法

    autoload() 方法可以设置每个页面访问时自动加载的对象,比如自动加载 JavaScript 代码,自动加载 Ajax 代码等等 注意此方法只负责加载 JavaScript/Ajax 代码,不执 ...

  9. 机器学习实战-KNN

    KNN算法很简单,大致的工作原理是:给定训练数据样本和标签,对于某测试的一个样本数据,选择距离其最近的k个训练样本,这k个训练样本中所属类别最多的类即为该测试样本的预测标签.简称kNN.通常k是不大于 ...

  10. poj_1042 贪心算法

    poj 1042 gone fishing 题目要求: 由有n个湖, 按照顺序排列,一个人从第一个湖向最后一个湖行进(方向只能从湖0到湖n-1),途中可以在湖中钓鱼.在每个湖中钓鱼时,开始的5分钟内可 ...