E. Hexagons
题目连接:
http://codeforces.com/contest/615/problem/E
Description
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.
Sample Input
3
Sample Output
-2 0
Hint
题意
有一个人会在坐标上按照六边形那样走,会一直走下去
问你走了n步之后,这个人会在哪儿。
题解:
就不停的讨论讨论……
首先我们先确定他在第几个环上,我们将环切成6份,然后再看他在哪一份上
然后再计算就好了……
不停讨论讨论
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n;
while(scanf("%lld",&n)!=EOF){
if(n==0)return puts("0 0");
n--;
for(long long i=1;;i++)
{
long long cnt = i*6;
if(n>=cnt)n-=cnt;
else
{
if(n<i)
{
printf("%lld %lld
",i*2-1-n,2+2*n);
break;
}
n-=i;
if(n<i)
{
printf("%lld %lld
",i-2-2*n,2*i);
break;
}
n-=i;
if(n<i)
{
printf("%lld %lld
",-1-i-n,2*i-2*n-2);
break;
}
n-=i;
if(n<i)
{
printf("%lld %lld
",-2*i+1+n,-2+(-2)*n);
break;
}
n-=i;
if(n<i)
{
printf("%lld %lld
",2-i+2*n,-2*i);
break;
}
n-=i;
printf("%lld %lld
",i+n+1,2-2*i+2*n);
break;
}
}
}
return 0;
}