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

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

Java AC 代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
int n = 0;
int[] result;
boolean[] marked = new boolean[10000000];
while((n = sc.nextInt()) != -1) {
result = new int[n + 1];
for(int i = 1; i < n + 1; i++) {
int a = result[i - 1] - i;
if(a > 0 && !marked[a]) {
result[i] = a;
marked[a] = true;
}
else {
result[i] = a + 2 * i;
marked[a + 2 * i] = true;
}
}
System.out.println(result[n]);
for(int i = 0; i < 10000000; i++ )
marked[i] = false;
}
}
}

poj 2081 Recaman's Sequence (dp)的更多相关文章

  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&#39;s Sequence

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

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

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

  5. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  6. POJ-2081 Recaman's Sequence

    Recaman's Sequence Time Limit: 3000MS Memory Limit: 60000K Total Submissions: 22392 Accepted: 9614 D ...

  7. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  8. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

  9. poj 3254(状态压缩DP)

    poj  3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...

随机推荐

  1. 五十二:WTForms表单验证之基本使用

    作用:1.做表单验证,把用户提交的数据验证是否合法2.做模板渲染 安装:pip install wtforms 表单验证1.自定义一个表单类,继承wtforms.Form2.定义好需要验证的字段,字段 ...

  2. 有道自然语言翻译和文字识别OCR(图片文字识别)接口调用

    官网 http://ai.youdao.com 文档地址 http://ai.youdao.com/docs/doc-ocr-api.s#p01 在Python中调用api. #/usr/bin/en ...

  3. 【AMAD】python-goose -- HTML Content/Article 提取器

    动机 简介 用法 个人评分 动机 新闻网页,结构大多是类似的. 所以,能不能用一种通用的爬取方法来提取其中的数据? 简介 Goose最初是一个Java项目,在2011年被转为了scala项目1. Py ...

  4. Linux C/C++基础——Windows远程登录Linux

    首先介绍两个ubuntu系统管理命令,用来测试连通性,及获取IP地址. 1.ping ping命令用来测试远程主机的连通性 使用方法:ping [参数] 远程主机IP地址 参数 功能 -a 每次相应时 ...

  5. aws和ufile挂载数据盘EBS

    aws的话挂载的ebs需要格式化,参考:https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/ebs-using-volumes.html ...

  6. c++ 读入优化通用模板

    struct ioss { #define endl '\n' ; char obuf[LEN], *oh = obuf; std::streambuf *fb; ioss() { ios::sync ...

  7. [转帖]超能课堂:Intel平台芯片组变迁史

    https://www.expreview.com/69419.html 现在我们在购买电脑时一个最重要的硬件就是主板了.作为承载CPU等主要硬件的平台,其最重要的功能就是连接外围设备,扩展计算机.但 ...

  8. 好的python链接

    海艳师姐博客园:  https://www.cnblogs.com/haiyan123/p/8387770.html

  9. asp.net之后台使用根目录运算符

    在asp.net前台,大家会经常使用根目录运算符~.这样,可以不用考虑网站的配置目录. 有时,需要在后台设置路径,同样需要使用根目录运算符.好吧,其实我每次需要使用这种方法,就需要在baidu上查找如 ...

  10. Docker守护进程

    Docker安装完成之后, 需要确定Docker的守护进程是否已经运行. Docker是使用root 权限运行他的程序,进而可以处理普通用户无法完成的操作(比如挂载文件系统). docker程序是Do ...