ACM学习历程—HDU1030 Delta-wave(数学)

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

题目大意就是有数字a走到数字b的最小步数,走的时候只能跨线走,不能跨点走。

首先我想到一个错误的想法,就是不妨设a<b,a先走斜线到b的同一层,然后再往b走。

但是发现一些例子不满足。

也就是这种折型走法不行。

但是通过上面的走法发现,可以走一个平行四边形的走法,就是a先走到c,c再走到b。

不过这次的不同是a是走斜线到c,c再走斜线到b。

首先第一次的走法发现了,从a走到b同一层的最短路径就是斜线。例如(1->3->2->6->5...),一路走过的都是最短走到的。

那么很容易证明第二种走法就是最短路了。

然后就是数学计算了,先根据每层的数字数目是个等差数列,然后根据不等式判断出a和b的层数,然后通过解方程得出c的位置,然后就是计算了,需要对情况中步数需要+1的情况特殊处理一下。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long

using namespace std;

int n, m;

int getLow(int x)
{
    int i = sqrt(x);
    while ((i-1)*(i-1) >= x) i--;
    while (i*i < x) i++;
    return i;
}

void work()
{
    int kn, km, kp, in, im, ip, ans;
    kn = getLow(n);
    km = getLow(m);
    in = n-(kn-1)*(kn-1);
    im = m-(km-1)*(km-1);
    if (km == kn)
        ans = abs(im-in);
    else if (in > im || 2*kn-in > 2*km-im)
    {
        ans = 2*(km-kn)-in%2+abs(in+in%2-im);
        in = 2*kn-in;
        im = 2*km-im;
        ans = min(ans, 2*(km-kn)-in%2+abs(in+in%2-im));
    }
    else
    {
        ip = in+in%2;
        im = 2*km-im;
        kp = (ip+im+im%2)/2;
        ans = 2*(kp-kn)-in%2+abs(in+in%2-ip);
        ip = 2*kp-ip;
        ans += 2*(km-kp)-ip%2+abs(ip+ip%2-im);
    }
    printf("%d\n", ans);
}

int main()
{
    //freopen("test.in", "r", stdin);
    while (scanf("%d%d", &n, &m) != EOF)
    {
        if (n > m) swap(n, m);
        work();
    }
    return 0;
}
时间: 2024-10-17 21:25:12

ACM学习历程—HDU1030 Delta-wave(数学)的相关文章

ACM学习历程—HDU5587 Array(数学 &amp;&amp; 二分 &amp;&amp; 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后从0到末尾,每一个都加上1. 例如:a0, a1, a2 => a0, a1, a2, 1, a0+1, a1+1, a2+1 题解中是这么说的:“ 其实Ai为i二进制中1的个数.每次变化A{k+2^i}=A{k}+1,(k<2^?i??)不产生进位,二进制1的个数加1.然后数位dp统计前m个数二

ACM学习历程—HDU5490 Simple Matrix (数学 &amp;&amp; 逆元 &amp;&amp; 快速幂) (2015合肥网赛07)

Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progression and sequence in the form of bn=b1qn−1(q>1,b1≠0) is called geometric progression. Huazheng wants to use these two simple sequences to generate a simp

ACM学习历程——HDU4472 Count(数学递推) (12年成都区域赛)

Description Prof. Tigris is the head of an archaeological team who is currently in charge of an excavation in a site of ancient relics.        This site contains relics of a village where civilization once flourished. One night, examining a writing r

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程—HDU 5073 Galaxy(数学)

Description Good news for us: to release the financial pressure, the government started selling galaxies and we can buy them from now on! The first one who bought a galaxy was Tianming Yun and he gave it to Xin Cheng as a present. To be fashionable,

ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns. Every day after work, Edward will place

ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)

Description Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ratio (the irrational number (1+√5)/2 ≍ 1.61803399 symbolized by the Greek letter φ) as its base. It is sometimes referred to as base-φ, golden mean b