HDU-1030 Delta-wave

Delta-wave

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

Problem Description

A triangle field is numbered with successive integers in the way shown on the picture below.

Alt
The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller’s route.

Write the program to determine the length of the shortest route connecting cells with numbers N and M.

Input

Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).

Output

Output should contain the length of the shortest route.

Sample Input

1 5
1 6
1 7

Sample Output

4
3
4

Reference Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct coordinate{
    ll x,y,z;
    void init(ll n){
        x=ceil(sqrt(n));
        y=(n+(2-x)*x)/2;
        z=(x*x-n)/2+1;
    }
    ll operator-(const coordinate &oth)const{
        return abs(x-oth.x)+abs(y-oth.y)+abs(z-oth.z);
    }
}M,N;
ll m,n;
int main(){
    while(scanf("%lld%lld",&m,&n)==2){
        M.init(m);N.init(n);
        printf("%lld\n",M-N);
    }
}

Tips

1.这条应该是智商题,因为范围给太大了,直接搜索估计会超时。
2.通过观察,发现可以从三个角度看这个三角形中的数,即层、左列、右列。这么看也是考虑到对称性,首先,层数肯定是要考虑的;然后会发现,如果两个数是在同一个“斜么叉”的列里,比如2 6 7 13 14……,就可以直接无脑往下走;再进一步考虑数字不在一个一个“斜么叉”的列里的情况以及另一个“斜么叉”的列,就能“显然”猜出公式,详见代码,其中x表示层数,y表示左边看的列,z表示右边看的列。
3.特别注意,推出的公式打出来还是不是原来的样子就很难说了。我本来以为两个整数m,n的m/n就是 ⌊ m n ⌋ \left\lfloor\frac mn\right\rfloor nm,其实是 sgn ( m n ) ⌊ ∣ m n ∣ ⌋ \text{sgn}(mn)\left\lfloor|\frac mn|\right\rfloor sgn(mn)nm……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值