Recaman's Sequence
64-bit integer IO format: %lld Java class name: Main
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 last line contains an integer −1, which should not be processed.
Output
Sample Input
7 10000 -1
Sample Output
20 18658
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <stack> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <ctime> 12 #define LL long long 13 #define INF 0x3f3f3f3f 14 #define pii pair<int,int> 15 16 using namespace std; 17 const int maxn = 500010; 18 int dp[maxn]; 19 bool vis[5000000]; 20 struct node { 21 int k,ans,o; 22 }; 23 node inp[1000000]; 24 bool cmp1(const node &x,const node &y) { 25 return x.k < y.k; 26 } 27 bool cmp2(const node &x,const node &y) { 28 return x.o < y.o; 29 } 30 int main() { 31 int tot = 0,tmp,cnt; 32 for(int i = 1; i < maxn; ++i) { 33 dp[i] = dp[i-1]-i; 34 if(dp[i] <= 0 || vis[dp[i]]) 35 dp[i] = dp[i-1]+i; 36 vis[dp[i]] = true; 37 } 38 while(~scanf("%d",&tmp)&&(~tmp)) { 39 inp[tot].k = tmp; 40 inp[tot].o = tot++; 41 } 42 sort(inp,inp+tot,cmp1); 43 for(int i = cnt = 0; i < maxn; ++i) 44 while(i == inp[cnt].k) inp[cnt++].ans = dp[i]; 45 sort(inp,inp+tot,cmp2); 46 for(int i = 0; i < tot; ++i) 47 printf("%d\n",inp[i].ans); 48 return 0; 49 }