POJ 2081

简介: Recaman's Sequence Time Limit: 3000MS   Memory Limit: 60000K Total Submissions: 18575   Accepted: 7751 Description The Recaman's...
Recaman's Sequence
Time Limit: 3000MS   Memory Limit: 60000K
Total Submissions: 18575   Accepted: 7751

Description

The Recaman's sequence is defined by a0 = 0 ; for m > 0, a m = a m−1 − m if the rsulting a m is positive and not already in the sequence, otherwise a m = a m−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 a k.

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 a k to the output.

Sample Input

7
10000
-1

Sample Output

20
18658
#include <stdio.h>
#include <string.h>
const int N=500010;
int ch[N];
bool vis[N*10];//状态数组一定要开得比数字数组大,否则测试时会RE 
void init()
{
    int i,j;
    memset(vis,false,sizeof(vis));
    memset(ch,0,sizeof(ch));
    vis[0]=vis[1]=vis[3]=true;
    ch[0]=0;ch[1]=1;
    for(i=2;i<N;i++)
    {
        ch[i]=ch[i-1]-i;
        if(ch[i]<1||vis[ch[i]])
            ch[i]=ch[i-1]+i;
        vis[ch[i]]=true;
    }
    return ;           
}
int main()
{
    int i,j,k;
    init();
    while(scanf("%d",&k),k!=-1)
        printf("%d\n",ch[k]);
    return 0;
}
    
    
    

 

目录
相关文章
|
5月前
poj-1611-The Suspects
poj-1611-The Suspects
26 0
|
5月前
Hopscotch(POJ-3050)
Hopscotch(POJ-3050)
|
12月前
poj 3298 数状数组
题目大意是一条大街上住着n个乒乓球爱好者,他们的水平高低用一个数值表示,他们经常举办比赛,比赛要三个人,一人当裁判。对裁判是有一定要求的,裁判的水平必须介于两选手之间且必须住他们中间,计算可以举办多少场比赛
40 0
poj 2299 求逆序数
http://poj.org/problem?id=2299 #include using namespace std; int aa[500010],bb[500010]; long long s=0; void merge(int l,int m,int r) { ...
793 0
POJ 2262 Goldbach&#39;s Conjecture
Problem Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the foll...
1005 0
|
机器学习/深度学习 算法
|
算法 计算机视觉
最小割-poj-2914
poj-2914-Minimum Cut Description Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must b
1558 0
|
机器学习/深度学习