ZOJ - 2421 Recaman's Sequence(打表水题)
发布日期:2021-10-03 15:44:41 浏览次数:1 分类:技术文章

本文共 651 字,大约阅读时间需要 2 分钟。

题目大意:A0 = 0

Am = A(m-1) - m,如果Am小于0或者Am前面已经出现过了,那么Am = A(m-1) + m

解题思路:打表水题

我用的是map,纪录数是否出现过了

#include 
#include
#include
using namespace std;const int N = 500010;typedef long long LL;map
Map;LL num[N];void init() { num[0] = 0; Map[0] = 1; LL t; for (int i = 1; i < N; i++) { t = num[i - 1] - i; if (t >= 0 && !Map[t]) { Map[t] = 1; num[i] = t; } else { t = num[i - 1] + i; Map[t]= 1; num[i] = t; } }}int main() { init(); int n; while(scanf("%d", &n) && (~n)) printf("%lld\n", num[n]); return 0;}

转载地址:https://blog.csdn.net/L123012013048/article/details/48860637 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:HDU - 1200 To and Fro(水)
下一篇:ZOJ - 3469 Food Delivery(区间DP)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月29日 12时27分21秒