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-)*(i-) >= 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-)*(kn-);
im = m-(km-)*(km-);
if (km == kn)
ans = abs(im-in);
else if (in > im || *kn-in > *km-im)
{
ans = *(km-kn)-in%+abs(in+in%-im);
in = *kn-in;
im = *km-im;
ans = min(ans, *(km-kn)-in%+abs(in+in%-im));
}
else
{
ip = in+in%;
im = *km-im;
kp = (ip+im+im%)/;
ans = *(kp-kn)-in%+abs(in+in%-ip);
ip = *kp-ip;
ans += *(km-kp)-ip%+abs(ip+ip%-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 ;
}

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后 ...

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

    Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progressio ...

  3. ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)

    Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...

  4. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  5. ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)

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

  6. ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)

    题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...

  7. ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...

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

    Description Good news for us: to release the financial pressure, the government started selling gala ...

  9. ACM学习历程—FZU2191完美的数字(数学)

    Description Bob是个很喜欢数字的孩子,现在他正在研究一个与数字相关的题目,我们知道一个数字的完美度是 把这个数字分解成三个整数相乘A*A*B(0<A<=B)的方法数,例如数字 ...

随机推荐

  1. lucas定理证明

    Lucas 定理(证明) A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]. 则组合数C(A,B)与C(a[n],b[n])* ...

  2. 【python】-- 模块、os、sys、time/datetime、random、logging、re

    模块 模块,用一堆代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...

  3. js:深入函数的定义

    函数定义方式: 1.function fun1(){alert("fun1");}  //函数就是一个很特殊的对象.是一个Function的实例.事实上在内存中存储的操作是通过一个 ...

  4. 机器学习Coursera学习总结

    Coursera上Andrew NG的机器学习实在是太火了,最近有时间花费了20来天的时间(每天3小时左右)终于学习完了全部的课程,总结如下: (1)适合入门,讲的比较基础,Andrew讲的很棒: ( ...

  5. HDU - 5550 Game Rooms 【DP+前缀和】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5550 题意 一撞大楼有N层楼,然后每层楼都有一部分人喜欢打羽毛球,一部分人喜欢打乒乓球 但是每层楼只能 ...

  6. .net序列化与反序列化——提供多次存储对象集后读取不完全解决方案

    ||问题: 文本文档读取序列化文件时只能读取第一次序列化对象或对象集,而多次序列化存到同一个文本文件中不能完全读取.最近做一个简单的学生管理系统,涉及到多次将学生对象序列化后追加存储到同一个文档中.在 ...

  7. 【leetcode刷题笔记】Substring with Concatenation of All Words

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  8. 新手用的git配置命令

    新手用的git配置命令 /**第一次链接远程仓库 本地已有项目需要上传码云 */ //1.配置码云用户名 git config --global user.name "昵称" // ...

  9. 第一篇 先用socket模拟web服务器

    一.用socket来模拟网站访问 socket为python2.7 #!/usr/bin/env python # -*- coding:utf-8 -*- import socket def han ...

  10. 剑指offer——圆圈中最后剩下的数字

    1.如果通过环形列表去模拟圆圈的话,最后时间复杂度为O(mn),而且还需要一个辅助链表来模拟圆圈,空间复杂度为O(n). 2.通过找出递推公式的方法,求得递推公式为 时间复杂度为O(n),空间复杂度为 ...