题意:按题目给出的搜索方式给出第n个点的坐标。
思路:首先确定第n个点在哪一层?第k层有6k个点,所以前k层总共有3*k*(k+1)个点。二分找出n在哪一层之后。就可以把这一层分成6块,找出n在一块中,然后根据每一块相邻点的变化规律就可以找到第n个点的坐标。
http://codeforces.com/contest/615/problem/E
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = (a) ; i <= (b) ; i ++)
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;
typedef long long LL;
typedef unsigned long long ULL;
int T,m,k;
LL n;
int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};
LL ef() {
LL left = 1 , right = 2e9;
LL mid;
while(left < right) {
mid = (left + right) >> 1;
if((LL)3*mid*(mid+1) < n) {
left = mid + 1;
}
else {
right = mid ;
}
}
return left;
}
void input() {
}
void solve() {
if(n == 0) {puts("0 0"); return ;}
LL pos = ef();
LL now = n - (LL)3 * (pos-1) * pos;
LL mark ;
now ++;
if(now == (LL) (pos * 6 + 1)) now = 1;
rep(i,1,7) {
if(now <= (LL)i * pos) {
mark = i;
break;
}
}
LL x,y;
now -= (mark-1) * pos;
now -= 1;
switch(mark) {
case 1:x = 2 * pos ; y = 0 ; x -= now ; y += 2 * now; break;
case 2:x = pos ; y = 2 * pos ; x -= 2 * now ; break;
case 3:x = -pos ; y = 2 * pos ; x -= now ; y -= 2 * now;break;
case 4:x = -2 * pos ; y = 0 ; x += now ; y -= 2 * now;break;
case 5:x = -pos ; y = -2 * pos ; x += 2 * now ; break;
case 6:x = pos ; y = -2 * pos ; x += now ; y += 2 * now; break;
}
printf("%I64d %I64d\n",x,y);
}
int main(void) {
while(~scanf("%I64d",&n)) {
input();
solve();
}
return 0;
}