# Delta-wave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4684    Accepted Submission(s): 1777

Problem Description

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

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

6 12

Sample Output

3

Source

Ural Collegiate Programming Contest 1998

``` 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<cmath>
6 using namespace std;
7 int sum(int m,int &cc)
8 {
9
10   int rr=(int)sqrt(1.0*m);  //确定她所在第几行..一右边为参数
11     if(rr*rr!=m)    rr++;
12      cc=rr*rr-m;
13     if(cc&1)
14       return 2*(rr-1);
15     else
16         return 2*rr-1;
17 }
18
19 int main()
20 {
22  while(cin>>m>>n)
23    {
24        int rankn=sum(n,ncc);   //得到n的所在第几行
25        int rankm=sum(m,mcc);   //得到m的所在第几行
26            ncc>>=1;            //得到n所在第几列，以右边为参数
27            mcc>>=1;            //得到m所在第几列，以右边为参数
28    //此处可以进一步优化...现在就不优化了..
29            if(rankn==rankm)
30            cout<<abs(n-m)<<endl;
31        else
32            if(rankn<rankm)
33            {
34
35               if(mcc>=ncc&&mcc<=(rankm-rankn)/2+ncc)
36                       cout<<(rankm-rankn)<<endl;
37               else
38               {
39
40                  if(2*mcc<(rankm-rankn)/2+2*ncc)
41                 {
44                  }
45                   else
46                   {
49                   }
50
51               }
52            }
53         else
54         {
55             //rankn>rankm
56             if(ncc>=mcc&&ncc<=(rankn-rankm)/2+mcc)
57              cout<<(rankn-rankm)<<endl;
58            else
59            {
60                if(2*ncc<(rankn-rankm)/2+2*mcc)
61                {
64                }
65                else
66                {
69                }
70            }
71         }
72    /* cout<<0&1<<endl;*/
73 }
74
75    return 0;
76 }```

657 篇文章64 人订阅

0 条评论

## 相关文章

35560

### HDU 1000 A + B Problem(指针版)

A + B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276...

28340

### HDU 5877 2016大连网络赛 Weak Pair(树状数组，线段树，动态开点，启发式合并，可持久化线段树)

Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144...

409100

34740

### 深入分析Spring MVC中RequestBody与ResponseBody

在SpringMVC中，可以使用@RequestBody和@ResponseBody两个注解，分别完成请求报文到对象和对象到响应报文的转换。在Sprin...

31310

### HDU 5157 Harry and magic string(回文树)

Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

37580

### Educational Codeforces Round 44 (Rated for Div. 2)A. Chess Placing

You are given a chessboard of size 1 × n. It is guaranteed that n is even. The c...

13120

### VF

Vasya is the beginning mathematician. He decided to make an important contributi...

9710

### HDUOJ 2672---god is a girl 《斐波那契数》

god is a girl Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3276...

28960

### HDUOJ---The Moving Points

The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/...

33540