【38.46%】【codeforces 615E】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

【题解】

规律题;
这里写图片描述
可以看到,每一圈的边数是等差数列;公差为6;
且边的长度和圈数有着对应的关系(除了>形的地方);
对于所给的n;
用二分搞出这是第几圈;
(an=6+(n-1)6,sn=(a1+an)n/2;->sn=3*n(n+1));
然后总数减去3*(key-1)*(key-1+1)key为n所在的圈数;
减掉之后的步数可以再模拟一下具体在哪个位置,找下规律就好;
坐标和圈数和剩余的步数mod圈数有关

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long

using namespace std;

//const int MAXN = x;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);

LL n;

void input_LL(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void input_int(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}


int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    input_LL(n);
    if (n==0)
    {
        puts("0 0");
        return 0;
    }
    LL l = 0,r = 1e9,m = 0;
    while (l <= r)
    {
        LL mid = (l+r)>>1;
        if (1LL*3*mid*(mid+1)<=n)
        {
            m = mid;
            l = mid+1;
        }
        else
            r = mid-1;
    }
    n-=3*m*(m+1);
    if (n==0)
    {
        printf("%I64d 0\n",m*2);
        return 0;
    }
    m++;
    LL zc = n/m,yu = n % m;
    //cout << zc << " "<<yu<<endl;

    switch (zc)
    {
    case 0:
        printf("%I64d %I64d\n",2*m-yu,2*yu);
        break;
    case 1:
        printf("%I64d %I64d\n",m-2*yu,2*m);
        break;
    case 2:
        printf("%I64d %I64d\n",-m-yu,2*m-yu*2);
        break;
    case 3:
        printf("%I64d %I64d\n",-2*m+yu,-2*yu);
        break;
    case 4:
        printf("%I64d %I64d\n",-m+2*yu,-2*m);
        break;
    case 5:
        printf("%I64d %I64d\n",m+yu,-2*m+yu*2);
        break;
    case 6:
        printf("%I64d %I64d\n",2*m,0);
        break;
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7632093.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值