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点 ...
随机推荐
- 录音整理文字工具otranscribe简介
网址: http://otranscribe.com/ 首先载入音频文件,支持 mp3, ogg, webm, wav (HTML5 无需将文件上传至服务器,可保护隐私),然后就可以边听边整理了. 通 ...
- Lock和Condition
1 什么是可重入锁 可重入锁是说一个线程在已经获取了该锁的情况下,还可以再次获取该锁. 主要的应用场景: 可重入锁指的是在一个线程中可以多次获取同一把锁,比如:一个线程在执行一个带锁的方法,该方法中又 ...
- python 列表中字符串排序故事一则
a = ["bca","cab","abc"] 有时候需要对列表排序 如果是对列表中整个元素 直接用sort()排序 如果想按元素的某一段排 ...
- docker centos yum 源
aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...
- 又一次发现Oracle太美之awr相关脚本简介
又一次发现Oracle太美之awr相关脚本简介 大家知道在$ORACLE_HOME/rdbms/admin下,有例如以下的相关脚本(我的环境为11.2.0.4.2): [oracle@rh64 ~]$ ...
- web audio living
总结网页音频直播的方案和遇到的问题. 代码:(github,待整理) 结果: 使用opus音频编码,web audio api 播放,可以达到100ms以内延时,高质量,低流量的音频直播. 背景: V ...
- linux 11 -- mount,umount
Linux 文件系统是一个以 / 为根的大树,我们在不同的设备和分区上都有文件系统.我们如何处理这种明显的不一致性?根 (/) 文件系统是在初始化过程中挂载的.您创建的其他每个文件系统在挂载 在挂载点 ...
- ssl和tls
HTTP 是一个网络协议,是专门用来帮你传输 Web 内容 SSL 是Secure Sockets Layer 为啥要发明 SSL 这个协议捏?因为原先互联网上使用的 HTTP 协议是明文的,存在很多 ...
- 20145229实验三实验报告——敏捷开发与XP实践
20145229实验三实验报告--敏捷开发与XP实践 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 ** 实验步骤**### 敏捷开发与XP 软件工程包括下列领域:软件需求 ...
- Ansible Ad-Hoc命令集
Ad-Hoc Ad-Hoc就是 “临时命令”, 从功能上讲 Ad-Hoc跟Ansible-playbook都差不多,Ansible提供了两种完成任务的方式: Ad-Hoc命令集与Ansible-pla ...