[Codeforces 615E] Hexagons (找規律)
阿新 • • 發佈:2019-02-01
Codeforces - 615E
給定一個六邊形網格,從原點出發逆時針繞行
問走了 N步以後的座標
找規律題,有點噁心
數出走了 N步以後,各方向向量的個數
發現每個方向向量個數的規律都是一個等差數列
然後數一下每個等差數列最多能有多少項,然後處理一下邊界
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define SQR(a) ((a)*(a))
#define PCUT puts("\n----------")
LL N;
LL Get(LL,LL);
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
while (~scanf("%lld", &N))
{
LL x=0, y=0;
if(N==0)
{
cout << x << " " << y << endl;
continue;
}
LL res;
res = Get(2,N); //-2,0
x+=res*(-2);
res = Get(3,N); //-1,-2
x+=res*(-1), y+=res*(-2);
res = Get(4,N); //1,-2
x+=res, y+=res*(-2);
res = Get(5,N); //2,0
x+=res*2;
res = Get(7,N-1); //-1,2
x+=res*(-1), y+=res*2;
res = Get(0,N-1); //1,2
x+=res, y+=res*2;
cout << x << " " << y << endl;
}
return 0;
}
LL Get(LL a, LL N)
{
LL l=0, r=1e9;
while(l<r)
{
LL mid=(l+r+1)>>1;
if(a*mid+3*mid*(mid-1)<=N) l=mid;
else r=mid-1;
}
LL n = l, res=n*(n+1)/2, tem;
// if(a==0) N++;
tem = N-(a*n+3*n*(n-1))-(a+6*n - (n+1));
if(tem<0) tem=0;
// printf("a:%lld n:%lld tot:%lld\n", a, n, res+tem);
return res + tem;
}
/*
1,2
0:0 1 4:0 2 9:0 3
-1,2
7:0 1 11:0 2 16:0 3
-2,0
1:0 1 6:0 2 11:0 3
-1,-2
2:0 1 7:0 2 12:0 3
1,-2
3:0 1 8:0 2 13:0 3
2,0
4:0 1 9:0 2 14:0 3
*/