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;
}
    
    
    

 

目录
相关文章
|
7月前
poj-1611-The Suspects
poj-1611-The Suspects
32 0
|
7月前
|
算法
Highways(POJ—2485)
Highways(POJ—2485)
POJ 1936 All in All
POJ 1936 All in All
78 0
|
机器学习/深度学习
棋盘问题 POJ 1321
总时间限制:  1000ms 内存限制:  65536kB 描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
1186 0
POJ 2027 No Brainer
Problem Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indicating the number of data sets.
870 0
|
机器学习/深度学习 算法
|
算法 数据建模 机器学习/深度学习
poj1273Drainage Ditches
1 #include 2 /* 3 题意:就是寻找从源点到汇点的最大流! 4 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 5 就是这两个点之间的流量了! 6 ...
853 0
|
算法 机器人 编译器
POJ-2632
#include int main() { int k,a,b,n,m,i,j,num,rep,rect[100][100],robot[100][3]; int flag; char c; for(scanf("%d...
932 0