您的位置:首页 > 编程语言 > Go语言

CodeForces 615E——Hexagons(二分,模拟)

2016-08-06 22:47 741 查看
E. Hexagons

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Ayrat is looking for the perfect code. He decided to start his search from an infinite field tiled by hexagons. For convenience the coordinate system is introduced, take a look at the picture to see how the coordinates of hexagon are defined:




Ayrat
is searching through the field. He started at point (0, 0) and is moving along the spiral (see second picture). Sometimes he forgets where
he is now. Help Ayrat determine his location after n moves.

Input

The only line of the input contains integer n (0 ≤ n ≤ 1018) —
the number of Ayrat's moves.

Output

Print two integers x and y —
current coordinates of Ayrat coordinates.

Examples

input
3


output
-2 0


input
7


output
3 2


唉,比赛完以后写一写确实挺水的。。

可以发现坐标都是一圈一圈的,每圈多走六步。

然后每条边的坐标变换规律,找一找就可以了。

所以先二分找到x轴上的那个最外圈的特殊点,再往回模拟就行了。

具体看代码,很好懂。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

const int MAXN = 10010;
const int INF=1000000007;
typedef long long ll;

ll n;

int main(){
cin>>n;
if(n==0){
printf("0 0\n");
return 0;
}
ll r=1000000000;
ll l=0;
while(r-l>1){
ll m=(r+l)>>1;
if(3*m*(m+1)<n){
l=m;
}
else{
r=m;
}
}
ll x=r*2,y=0;
ll t=3*r*(r+1)-n;
int flag=0;
while(t){
if(t<r){
switch(flag){
case 0:
x-=t;
y-=2*t;
flag=1;
break;
case 1:
x-=2*t;
flag=2;
break;
case 2:
x-=t;
y+=2*t;
flag=3;
break;
case 3:
x+=t;
y+=2*t;
flag=3;
break;
case 4:
x+=2*t;
flag=5;
break;
case 5:
x+=t;
y-=2*t;
flag=0;
break;
}
break;
}
switch(flag){
case 0:
x-=r;
y-=2*r;
flag=1;
break;
case 1:
x-=2*r;
flag=2;
break;
case 2:
x-=r;
y+=2*r;
flag=3;
break;
case 3:
x+=r;
y+=2*r;
flag=4;
break;
case 4:
x+=2*r;
flag=5;
break;
case 5:
x+=r;
y-=2*r;
flag=0;
break;
}
t-=r;
}
cout<<x<<" "<<y<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: