Codeforces 615E Hexagons 【找规律】

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.

Sample test(s)
input
3
output
-2 0
input
7
output
3 2



无语了,还以为是神题。没敢看。SB了,写二分时手残少写个= o(╯□╰)o

题意:一个人沿着图中走正六边形,问n步后在哪个坐标。


思路:分类讨论。。。。。。第i圈走6*i步,每圈分6段,一步步找就KO了。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 10007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#define first fi
#define second se
using namespace std;
LL Find(LL l, LL r, LL n)
{
    LL ans = 0;
    while(r >= l)
    {
        LL mid = (l + r) >> 1;
        if(3 * (mid + 1) * mid <= n)
        {
            ans = mid;
            l = mid+1;
        }
        else
            r = mid-1;
    }
    return ans;
}
int main()
{
    LL n; Rl(n);
    LL x, y;
    if(n == 0)
        x = 0, y = 0;
    else
    {
        LL r = 1000000000;
        LL t = Find(1, r, n); //Pl(t);
        LL yu = n - 3 * t * (t + 1);
        t++;
        if(yu == 0)
            x = 2*(t-1), y = 0;
        else
        {
            LL tt = yu / t;
            yu = yu - tt * t;
            //Pl(tt);
            if(tt == 0)
                x = 2*t - yu, y = 2*yu;
            else if(tt == 1)
                x = t - yu*2, y = 2*t;
            else if(tt == 2)
                x = -t - yu, y = 2*t - 2*yu;
            else if(tt == 3)
                x = -2*t + yu, y = -2*yu;
            else if(tt == 4)
                x = -t + 2*yu, y = -2*t;
            else if(tt == 5)
                x = t + yu, y = -2*t + 2*yu;
            else
                x = 2*t, y = 0;
        }
    }
    printf("%lld %lld\n", x, y);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值