[Codeforces 615E] Hexagons (找规律)

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
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值