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. bnuoj 29373 Key Logger(模拟双向队列)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=29373 [题意]:模拟光标输入 [题解]:用双向列表模拟实现,这里用其他模拟会超时,注意内存的释放 ...

  2. QR code 金庸小说体(二)

    传说 在二维码世纪,流传着这样一个传说,long long ago,武林一片混乱,这时魔教二长老创立了一门绝世武功——QR code,随后称霸武林.但同时也遭到武林中人的觊觎和反抗,各大武林正派掌门人 ...

  3. 团体程序设计天梯赛-练习集L1-010. 比较大小

    L1-010. 比较大小 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆(浙江大学城市学院) 本题要求将输入的任意3个整数从小 ...

  4. CrackMe_001

    本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注册机的东西. 其中,文章中按照如下逻辑编排(解决如 ...

  5. android中的category静态值(转)

    提供将要执行的action的额外信息,一般在隐式地启动activity时需要用到.常见的category如下 CATEGORY_ALTERNATIVE 设置这个activity是否可以被认为是用户正在 ...

  6. objective-c宏定义

    1.先来几个常用的: // 是否高清屏 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? ...

  7. HDU1565+状态压缩dp

    简单的压缩状态 dp /* 状态压缩dp 同hdu2167 利用滚动数组!! */ #include<stdio.h> #include<string.h> #include& ...

  8. APK签名校验绕过

    APK签名校验绕过 Android JNI 获取应用签名 android apk 防止反编译技术第一篇-加壳技术 android apk 防止反编译技术第五篇-完整性校验 利用IDA Pro反汇编程序 ...

  9. png24是支持Alpha透明的。。。。。。

    这个可能跟每个人使用切图软件有关. 1.Photoshop   1)只能导出布尔透明(全透明或者全不透明)的PNG8.    2)能导出alpha透明(全透明,全不透明,半透明)的PNG24.     ...

  10. 64位ubuntu安装WPS

    http://jingyan.baidu.com/article/d3b74d64afd96f1f77e609a3.html http://sixipiaoyang.blog.163.com/blog ...