比较简单,加一个B数组判重即可
Recaman‘s Sequence
Time Limit: 3000MS | Memory Limit: 60000K | |
Total Submissions: 21743 | Accepted: 9287 |
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
20 18658
Source
1 //oimonster 2 #include<cstdio> 3 #include<cstdlib> 4 #include<iostream> 5 using namespace std; 6 int a[500001],b[10000000]={0}; 7 int main(){ 8 int i,j,n,m; 9 a[0]=0; 10 b[0]=1; 11 for(i=1;i<=500000;i++){ 12 m=a[i-1]-i; 13 if((m<=0)||(b[m]==1)){ 14 a[i]=a[i-1]+i; 15 b[a[i]]=1; 16 } 17 else{ 18 a[i]=m; 19 b[m]=1; 20 } 21 } 22 scanf("%d",&n); 23 while(n!=-1){ 24 printf("%d\n",a[n]); 25 scanf("%d",&n); 26 } 27 return 0; 28 }
POJ 2081 Recaman's Sequence
时间: 2024-10-10 08:52:50