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. 浏览器登录cookie

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  2. Android创建library工程

    本文着重介绍如何创建Android library,并且在工程中使用此library提供的资源,具体步骤如下:1. 创建一个Android工程,命名为sourceProj2. 右键--properti ...

  3. c/c++一些小知识点(特此总结)

    ---恢复内容开始--- ---恢复内容结束---

  4. 大数据学习系列(1)-- linux之文件系统结构介绍

    1./ 根目录 --------- 所有目录挂在其下 2./boot --------- 存放Ubuntu内核和系统启动文件.系统启动时这些文件先被装载. 3./etc --------- 系统的配置 ...

  5. iOS开发之获取系统相册ALAssetLibrary

    注:当你选择看这篇博客时想必你的应用还支持iOS8一下系统,如果你的应用要求最低版本大于iOS8,建议使用PhotoKit框架,效率更高 ALAssetsLibrary包含,ALAssetsLibra ...

  6. 【python】-- 类的多继承、经典类、新式类

    继承知识点补充 在python还支持多继承,但是一般我们很少用,有些语言干脆就不支持多继承,有多继承,就会带来两个概念,经典类和新式类. 一.多继承 之前我们都是讲的单继承,那么什么是多继承呢?说白了 ...

  7. 洛谷 2216 [HAOI2007]理想的正方形

    题目戳这里 一句话题意 给你一个a×b的矩形,求一个n×n的子矩阵,矩阵里面的最大值和最小值之差最小. Solution 这个题目许多大佬都是单调队列,但是我不是很会,只好用了比较傻逼的方法: 首先我 ...

  8. hibernate 多对多操作(级联操作)

    一.级联保存 分别在用户和角色配置文件中在set标签中加入cascade="save-update": 示例:添加同时添加用户对象和角色对象并关联两个对象: 只需将角色放入用户对象 ...

  9. 如何用excel urldecode解码把url编码转为汉字?

    统计分析可以反映出网站运营的情况,并根据实际作出相应的调整,是站长必需的基础技能.ytkah感觉最好用的是谷歌统计,里面有个搜索关键词及对应受访页面,这个功能对优化用处很大,但大家都知道访问不太顺畅. ...

  10. 计算机网络协议层次(转发:http://blog.csdn.net/gavin_john/article/details/53186570)

    计算机网络学习的核心内容就是网络协议的学习.网络协议是为计算机网络中进行数据交换而建立的规则.标准或者说是约定的集合.计算机网络协议同我们的语言一样,多种多样. 为了给网络协议的设计提供一个结构,网络 ...