Recaman's Sequence
Time Limit: 3000MS   Memory Limit: 60000K
Total Submissions: 22363   Accepted: 9605

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

28

18658

对于这题的正确做法就是模拟加暴力Dp;

在DP的时候一定记得考虑时间复杂度的问题;

代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; const int maxn = +;
int a[maxn];
bool used[]; int main(void)
{
int k,i,j; a[] = ;
memset(used,,sizeof(used));
used[] = ;
for(i=;i<=;++i)
{
if(a[i-]-i>&&!used[a[i-]-i]) a[i] = a[i-]-i;
else a[i] = a[i-]+i;
used[a[i]] = ;
}
while(scanf("%d",&k),k!=-)
{ cout<<a[k]<<endl;
} return ;
}

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 (dp)

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

  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 2284 That Nice Euler Circuit 解题报告

    That Nice Euler Circuit Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 1975   Accepted ...

  6. poj 1094 Sorting It All Out 解题报告

    题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...

  7. Poj 1953 World Cup Noise之解题报告

    World Cup Noise Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16369   Accepted: 8095 ...

  8. POJ 1308 Is It A Tree? 解题报告

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32052   Accepted: 10876 D ...

  9. POJ 1958 Strange Towers of Hanoi 解题报告

    Strange Towers of Hanoi 大体意思是要求\(n\)盘4的的hanoi tower问题. 总所周知,\(n\)盘3塔有递推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i ...

随机推荐

  1. ubuntu下安装GTK过程

    GTK的安装过程比较繁琐,以前也安装过,但没有记录下来,Google一下记录下来备用. 利用此方法成功在ubuntu12.04下安装GTK 2.24.10 1.安装gcc/g++/gdb/make 等 ...

  2. shell复习笔记----查找与替换

    查找文档 以grep 程序查找文本(匹配文本 matching text)相当方便.传统上有三种程序可以用来查找整个文本文件. grep 最早的文本匹配程序.其最简单的方式就是使用固定字符串 $ wh ...

  3. HZNU1015: 矩阵排序

    http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1015 题意:把矩阵每一行都排序. (以前觉得很难的题目回头看看原来如此简单 ][]; ; i&l ...

  4. 在NEXUS中加入自己定义的第三方PROXIES代理库

    就是要等会耐心,更新好之后,才能在PUBLIC库里进行操作. 下图是JBOSS的

  5. Centos之LAMP环境搭建

    原文:http://blog.sina.com.cn/s/blog_c02ed6590101d2sl.html 一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@ ...

  6. (转)Android系统自带样式(@android:style/)

    在AndroidManifest.xml文件的activity中配置 1.android:theme="@android:style/Theme" 默认状态,即如果theme这里不 ...

  7. 安装 ArcGIS Runtime SDK for Android

    ArcGIS for Android 开发:Android 平台搭建 - liyong20080101的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/liyong ...

  8. Spring的依赖注入

    依赖注入—手工装配(XML方式)--通过属性注入(相应属性必须有setter方法才行,同时,要有无参构造方法): <!-- 通过属性注入(setter方法) --> <bean id ...

  9. CodeForces250B——Restoring IPv6(字符串处理)

    Restoring IPv6 DescriptionAn IPv6-address is a 128-bit number. For convenience, this number is recor ...

  10. CVE漏洞爬虫java代码依赖-TestNG

    TestNG是Java中的一个测试框架,而该CVE漏洞爬虫示例中所涉及到的java代码中, \Crawler\src\com\***\ThreaderRun.java文件在导入import org.t ...