标签:
Description
Input
Output
Sample Input
7 10000 -1
Sample Output
20 18658
【题意】第m数是根据第m-1数推出来。如果a[m-1]-m>0,并且a[m-1]-m在前面的序列中没有出现过那么a[m] = a[m-1]-m否则a[m] = a[m-1]+m
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int N=10000007; int a[500005]; bool ha[N]; int main() { memset(a,0,sizeof(a)); memset(ha,false,sizeof(ha)); for(int i=1;i<=500000;i++) { if(a[i-1]-i>0&&!ha[a[i-1]-i]) { a[i]=a[i-1]-i; } else a[i]=a[i-1]+i; ha[a[i]]=true; } int k; while(~scanf("%d",&k)) { if(k<0) break; printf("%d\n",a[k]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/iwantstrong/p/5935059.html