CodeForces 615E——Hexagons(二分,模拟)
2016-08-06 22:47
741 查看
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:
![](http://codeforces.com/predownloaded/9e/a2/9ea263a78534b6fe47dc74f1d288e95ec9d57297.png)
![](http://codeforces.com/predownloaded/0b/45/0b4511e1fdf0ad4fba29f7b2b57d54d5f24e5770.png)
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.
Examples
input
output
input
output
唉,比赛完以后写一写确实挺水的。。
可以发现坐标都是一圈一圈的,每圈多走六步。
然后每条边的坐标变换规律,找一找就可以了。
所以先二分找到x轴上的那个最外圈的特殊点,再往回模拟就行了。
具体看代码,很好懂。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int MAXN = 10010;
const int INF=1000000007;
typedef long long ll;
ll n;
int main(){
cin>>n;
if(n==0){
printf("0 0\n");
return 0;
}
ll r=1000000000;
ll l=0;
while(r-l>1){
ll m=(r+l)>>1;
if(3*m*(m+1)<n){
l=m;
}
else{
r=m;
}
}
ll x=r*2,y=0;
ll t=3*r*(r+1)-n;
int flag=0;
while(t){
if(t<r){
switch(flag){
case 0:
x-=t;
y-=2*t;
flag=1;
break;
case 1:
x-=2*t;
flag=2;
break;
case 2:
x-=t;
y+=2*t;
flag=3;
break;
case 3:
x+=t;
y+=2*t;
flag=3;
break;
case 4:
x+=2*t;
flag=5;
break;
case 5:
x+=t;
y-=2*t;
flag=0;
break;
}
break;
}
switch(flag){
case 0:
x-=r;
y-=2*r;
flag=1;
break;
case 1:
x-=2*r;
flag=2;
break;
case 2:
x-=r;
y+=2*r;
flag=3;
break;
case 3:
x+=r;
y+=2*r;
flag=4;
break;
case 4:
x+=2*r;
flag=5;
break;
case 5:
x+=r;
y-=2*r;
flag=0;
break;
}
t-=r;
}
cout<<x<<" "<<y<<endl;
}
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:
![](http://codeforces.com/predownloaded/9e/a2/9ea263a78534b6fe47dc74f1d288e95ec9d57297.png)
![](http://codeforces.com/predownloaded/0b/45/0b4511e1fdf0ad4fba29f7b2b57d54d5f24e5770.png)
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.
Examples
input
3
output
-2 0
input
7
output
3 2
唉,比赛完以后写一写确实挺水的。。
可以发现坐标都是一圈一圈的,每圈多走六步。
然后每条边的坐标变换规律,找一找就可以了。
所以先二分找到x轴上的那个最外圈的特殊点,再往回模拟就行了。
具体看代码,很好懂。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int MAXN = 10010;
const int INF=1000000007;
typedef long long ll;
ll n;
int main(){
cin>>n;
if(n==0){
printf("0 0\n");
return 0;
}
ll r=1000000000;
ll l=0;
while(r-l>1){
ll m=(r+l)>>1;
if(3*m*(m+1)<n){
l=m;
}
else{
r=m;
}
}
ll x=r*2,y=0;
ll t=3*r*(r+1)-n;
int flag=0;
while(t){
if(t<r){
switch(flag){
case 0:
x-=t;
y-=2*t;
flag=1;
break;
case 1:
x-=2*t;
flag=2;
break;
case 2:
x-=t;
y+=2*t;
flag=3;
break;
case 3:
x+=t;
y+=2*t;
flag=3;
break;
case 4:
x+=2*t;
flag=5;
break;
case 5:
x+=t;
y-=2*t;
flag=0;
break;
}
break;
}
switch(flag){
case 0:
x-=r;
y-=2*r;
flag=1;
break;
case 1:
x-=2*r;
flag=2;
break;
case 2:
x-=r;
y+=2*r;
flag=3;
break;
case 3:
x+=r;
y+=2*r;
flag=4;
break;
case 4:
x+=2*r;
flag=5;
break;
case 5:
x+=r;
y-=2*r;
flag=0;
break;
}
t-=r;
}
cout<<x<<" "<<y<<endl;
}
相关文章推荐
- Codeforces 615E Hexagons (Round #338 (Div. 2) E题) 二分答案+找规律
- 【CodeForces 615E】Hexagons
- Codeforces 490E Restoring Increasing Sequence【二分+模拟】细节题
- Codeforces 615E Hexagons 【找规律】
- 【38.46%】【codeforces 615E】Hexagons
- CodeForces - 803B 二分 模拟
- codeforces 460C - Present 二分加模拟
- codeforces 722系列ABCD 【套题】模拟+并查集+STL+数学二分
- CodeForces - 670D2 Magic Powder - 2 (二分&模拟)
- CodeForces 493C Vasya and Basketball(模拟 + 二分)
- [Codeforces 615E] Hexagons (找规律)
- CodeForces 615E Hexagons
- CodeForces-732D-Exams(二分 模拟 贪心)
- CodeForces 651A:Joysticks【模拟】
- Codeforces 825D 二分贪心
- 4000 CodeForces 810A Straight <<A>> 【数学】【模拟】
- CodeForces - 754B Ilya and tic-tac-toe game(模拟)
- CodeForces 703A Mishka and Game(模拟)
- Codeforces 749D Leaving Auction 二分+Set
- CodeForces-768B Code for 1 二分搜索线段