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点 ...
随机推荐
- thinkPHP5.0的学习研究【架构】
2017年6月19日18:51:53 架构:1.ThinkPHP5.0应用基于MVC(模型-视图-控制器)的方式来组织.2.MVC是一个设计模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC ...
- Cow Contest(传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10450 Accepted: 5841 Desc ...
- elasticsearch从入门到出门-03-多种搜索
1.query string search 2.query DSL 3.query filter 4.full-text search 5.phrase search 6.highlight sear ...
- 1.BeanFactory解析
package org.springframework.beans.factory; import org.springframework.beans.BeansException; import o ...
- TensorFlow_action
安装TensorFlow 包依赖 C:\Users\sas> pip3 install --upgrade tensorflow Collecting tensorflow Downloadi ...
- 【python】-- web开发之jQuery
jQuery jQuery 是一个 JavaScript 函数库,jQuery库包含以下特性(HTML 元素选取.HTML 元素操作.CSS 操作.HTML 事件函数.JavaScript 特效和动画 ...
- 区分Web前端和后端(转载)
转载自:http://blog.csdn.net/rosetta/article/details/53871766 前言 做C开发将近六年,基本上没有接触过web相关的东西,原来听别人说web相关 ...
- 7 Javascript:表单与验证-非空验证
表单提交前要检查数据的合法性 在要对表单里的数据进行验证的时候,能够利用getElementById()来訪问网页上不论什么一个元素 每一个表单域都有一个form对象,可被传给不论什么验证表单数据的函 ...
- Activiti使用过程_1
1 微信公众号:
- Linux内核设计基础(九)之进程管理和调度
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/BlueCloudMatrix/article/details/30799225 在Linux中进程用 ...