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. javascript之Screen(屏幕)对象

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 常用的CSS样式示例代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 根据XML文件 生成 java类

    最近一直在做关于webservice 的项目,这种项目最麻烦的就是根据对方的要求产生XML,文档里面虽然有XML结构,但是要转化为java里面的实体实在费劲, 有个自动化的工具就好了,半自动化也好,省 ...

  4. 【转】HBase shell命令与 scan 过滤器

    Hbase 常用shell命令 https://www.cnblogs.com/i80386/p/4105423.html HBase基础之常用过滤器hbase shell操作 https://www ...

  5. .net分流抢票助手

    官方网站: http://www.12306bypass.com/作者:Cheney.小风分流抢票基于.Net4.0框架开发,在Windows7之后的操作系统可直接打开.其他操作系统如打不开或者打开报 ...

  6. Java ——关键字 数据类型 变量 常量

    本节重点思维导图 Java程序结构 public class 类名 { public static void main(String[] args){ //……语句 } } 一门语言是由以下各种元素组 ...

  7. 【HANA系列】SAP HANA SQL合并多行操作

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL合并多行 ...

  8. random、json、pickle、hashlib、hmac、shutil、shevle模块

    今日内容: 1. random 模块 2. json模块 3. pickle 模块 4.hashlib 模块 5. hmac 模块 6. shutil 模块 7. shelve 模块 1. rando ...

  9. Linux常用目录名称

    目录 用途 / 虚拟目录的根文件,通常不会在这里存储文件 /bin 二进制目录,存放许多用户的GNU工具 /boot 启动目录,存放启动文件 /dev 设备目录,Linux在这里创建设备节点 /etc ...

  10. 【神经网络与深度学习】【C/C++】使用blas做矩阵乘法

    使用blas做矩阵乘法   #define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h> #include <st ...