codeforces 615E Hexagons (二分+找规律)
1 second
256 megabytes
standard input
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.
The only line of the input contains integer n (0 ≤ n ≤ 1018) — the number of Ayrat's moves.
Print two integers x and y — current coordinates of Ayrat coordinates.
3
-2 0
7
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 (二分+找规律)的更多相关文章
- Lieges of Legendre CodeForces - 603C (博弈论,SG找规律)
大意: 给定$n$堆石子, 两人轮流操作, 每次操作两种选择 $(1)$任选非空堆拿走一个石子 $(2)$任选石子数为$2x(x>0)$的一堆, 替换为$k$堆$x$个石子. ($k$给定) 最 ...
- 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有多少位数 ...
- 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! ...
- codeforces 353D 递推 找规律
题意:一组男生女生在排队,每秒钟所有排在女生左边的男生与她相邻的女生交换位置,求女生全部换到男生前面的时间. 思路: 解法一:队伍最前面的那些女生不需要交换,后面的女生有两种状态:畅通无阻,前一个女生 ...
- Educational Codeforces Round 8 B 找规律
B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces D. Queue 找规律+递推
题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...
随机推荐
- 什么是Java Server Pages?
JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结束. JSP是一种Java servlet ...
- winerror.h中的内容(可以查看last error对应)
/************************************************************************* ** winerror.h -- error co ...
- linux php.ini又一次载入问题
今天发现自己server改动php.ini之后无法又一次载入! .无法使用php-fpm reload,奇怪.! 后来查了一下.能够使用 /etc/init.d/php-fpm reload 来又一次 ...
- 论JavaWeb前后端分离放弃jsp
1.静态资源使用Nginx反向代理Tomcat,Tomcat挂了网站仍可访问.2.静态与后端服务器分离,提升性能.3.大并发情况下,可同时扩展前后端服务器.4.接口可复用至App相关服务.5.网站热部 ...
- python高级语法进阶
python中几个比较难懂概念进阶. 迭代器 实现了迭代器协议的容器对象,基于如下两个方法: __next__:返回容器的下一个元素 __iter__:返回迭代器本身 由此可见,如果要自定义一个迭代器 ...
- cocos2dx的ui封装
cocos2dx里加载cocosudio导出的ui配置文件,在这之上封装了一下,封装核心类包括 UIManager,UILayer,UIOwner UIManager是所有ui总的管理类,代码如下: ...
- Android N API预览
Android N for Developers 重要的开发人员功能 多窗体支持 通知 JIT/AOT 编译 高速的应用安装路径 外出瞌睡模式 后台优化 Data Saver 高速设置图块 API 号 ...
- 11.Django数据库操作(查)
django.db.models.query.QuerySet1.可迭代2.可切片 官方文档:https://docs.djangoproject.com/en/1.9/ref/models/quer ...
- Token Fund到底是什么?
相比传统的VC基金,作为新生事物的Token Fund从一出场便吸引着行业目光的追随.行业早期ICO促使区块链行业野蛮生长,大量区块链项目涌现,同时也带动了大量“代投”朝Token Fund方向发展. ...
- LeetCode:删除排序数组中的重复项||【80】
LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...