Poj 2081 Recaman's Sequence之解题报告
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.
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.
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的时候一定记得考虑时间复杂度的问题;
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 6 using namespace std; 7 8 const int maxn = 500000+10; 9 int a[maxn]; 10 bool used[3012500]; 11 12 int main(void) 13 { 14 int k,i,j; 15 16 a[0] = 0; 17 memset(used,0,sizeof(used)); 18 used[0] = 1; 19 for(i=1;i<=500000;++i) 20 { 21 if(a[i-1]-i>0&&!used[a[i-1]-i]) a[i] = a[i-1]-i; 22 else a[i] = a[i-1]+i; 23 used[a[i]] = 1; 24 } 25 while(scanf("%d",&k),k!=-1) 26 { 27 28 cout<<a[k]<<endl; 29 } 30 31 return 0; 32 }