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

题意:给你一个数n,是走的步数,问走了这么多步最后落点的坐标是多少;

思路:可以看出这些轨迹形成了一层一层的六边形,间距为2,先二分找出给的n最后落在第几层六边形上,再根据层数找到6个顶点跟层数的关系,最后判断最终位置的坐标,注意,在x的非负半轴最后要减2,因为一开始判断的时候这些点都判断多了一层;

AC代码:

#include <bits/stdc++.h>
using namespace std;
long long ansx,ansy,pos,n;
long long bisearch()
{
long long l=0,r=1e9,mid;
while(l<=r)
{
mid=((l+r)>>1);
if(3*mid*(mid+1)>n)r=mid-1;
else l=mid+1;
}
return l;
}
int check(long long x,long long y)
{
if(x==0)ansx=2*pos-y,ansy=2*y;
else if(x==1)ansx=pos-2*y,ansy=2*pos;
else if(x==2)ansx=-pos-y,ansy=2*pos-2*y;
else if(x==3)ansx=-2*pos+y,ansy=-2*y;
else if(x==4)ansx=-pos+2*y,ansy=-2*pos;
else if(x==5)ansx=pos+y,ansy=-2*pos+2*y;
return 0;
}
int main()
{
cin>>n;
pos=bisearch();
n-=3*pos*(pos-1);
check(n/pos,n%pos);
if(ansx>0&&ansy==0)ansx-=2;
cout<<ansx<<" "<<ansy<<"\n";
return 0;
}

codeforces 615E Hexagons (二分+找规律)的更多相关文章

  1. Lieges of Legendre CodeForces - 603C (博弈论,SG找规律)

    大意: 给定$n$堆石子, 两人轮流操作, 每次操作两种选择 $(1)$任选非空堆拿走一个石子 $(2)$任选石子数为$2x(x>0)$的一堆, 替换为$k$堆$x$个石子. ($k$给定) 最 ...

  2. Codeforces Round #219(Div. 2)373 B. Making Sequences is Fun(二分+找规律)

    题目意思大概是给你w,m,k三个数,让你从m开始找 m m+1 m+2 m+3...........m+m', 使得他们的权值之和不超过w,计算权值的方法如下S(n)·k . S(n)表示n有多少位数 ...

  3. Trailing Zeroes (III) LightOJ - 1138 二分+找规律

    Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! ...

  4. codeforces 353D 递推 找规律

    题意:一组男生女生在排队,每秒钟所有排在女生左边的男生与她相邻的女生交换位置,求女生全部换到男生前面的时间. 思路: 解法一:队伍最前面的那些女生不需要交换,后面的女生有两种状态:畅通无阻,前一个女生 ...

  5. Educational Codeforces Round 8 B 找规律

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  7. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  8. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  9. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

随机推荐

  1. JSP隐式对象是JSP容器为每个页面提供的Java对象

    JSP 隐式对象 JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明.JSP隐式对象也被称为预定义变量. JSP所支持的九大隐式对象: 对象 描述 reque ...

  2. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

  3. 右键打开cmd

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHere]@="Open cmd ...

  4. Netty ByteBuf(图解之 2)| 秒懂

    目录 Netty ByteBuf(图解二):API 图解 源码工程 写在前面 ByteBuf 的四个逻辑部分 ByteBuf 的三个指针 ByteBuf 的三组方法 ByteBuf 的引用计数 Byt ...

  5. Full卷积 Same卷积 Vaild卷积

  6. cocos2dx使用cocostudio导出的animation

    local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...

  7. shell脚本调试运行

    1.在命令行提供参数:$sh -x script.sh   但是有的shell脚本只能用 ./xxx.sh的方式运行,不能用sh命令解析执行.则此方法会报错.2.脚本开头提供参数:#!/bin/sh ...

  8. Excel 文件下载

    INCLUDE OLE2INCL * ALV输出   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'     EXPORTING       I_CALLBACK_PRO ...

  9. SVG嵌入HTML

    将SVG图像嵌入到HTML文件有多种方法: 使用<iframe>元素来嵌入SVG图像 使用<img>元素来嵌入SVG图像 将SVG图像作为背景图像嵌入 直接使用<svg& ...

  10. Swift 枚举简单使用

    //定义一个枚举 Direction 枚举字符名字 enum Direction{ case North case South case East case West }; enum Directio ...