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.

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;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值