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-)*(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(数学)的更多相关文章
- ACM学习历程—HDU5587 Array(数学 && 二分 && 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后 ...
- 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 ...
- ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)
Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. ...
- ACM学习历程—SNNUOJ1213 加油站问题(动态规划 || 数学)
题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1213 这是这次微软实习面试的一道题,当时只相出了一个2n的做法,面试官让我优化成n的做 ...
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- ACM学习历程—HDU 5073 Galaxy(数学)
Description Good news for us: to release the financial pressure, the government started selling gala ...
- ACM学习历程—FZU2191完美的数字(数学)
Description Bob是个很喜欢数字的孩子,现在他正在研究一个与数字相关的题目,我们知道一个数字的完美度是 把这个数字分解成三个整数相乘A*A*B(0<A<=B)的方法数,例如数字 ...
随机推荐
- LNMP环境搭建(一:nginx)
1.从nginx官网获取源码包 # cd /usr/local/src # wget http://nginx.org/download/nginx-1.10.3.tar.gz 2.解压源码包 # t ...
- PHP手机号码正则表达式
php用正则表达式判断手机号码的写法:从文章中匹配出所有的手机号就可以preg_match_all(),如果要检查用户输入的手机号是否正确可这样来检查:preg_match(). 用正则匹配手机号码的 ...
- x^a=b(mod c)求解x在[0,c-1]上解的个数模板+原根求法
/************************************* 求解x^a=b(mod c) x在[0,c-1]上解的个数模板 输入:1e9>=a,b>=1,1e9>= ...
- element-ui table 点击分页table滚动到顶部
在做项目中,碰到一个问题,table加了固定头,内容可滚动,当滚到table底边时,点击分页后还在底边 解决方法:设置table的 ref='multipleTable' //切换分页的方法加上下面这 ...
- [置顶] 2013_CSUST暑假训练总结
2013-7-19 shu 新生训练赛:母函数[转换成了背包做的] shuacm 题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083总结:h ...
- lua解析json
自己写的lua解析json,带容错,如果要是用于格式检查,得修改下.很简单直接贴代码 --------------------------------------------------json解析- ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
- Swift来了,是不是能够入手IOS开发了?
在今天的WWDC2014上,苹果公布了一种全新的Swift.在苹果高管 Craig Federighi 的描写叙述中,Swift 在各个方面优于 Objective-C,也不会有那么多复杂的符号和表达 ...
- SMARTFORMS自定义打印格式
[转自 http://lz357502668.blog.163.com/blog/static/16496743201272155135570/] 在sap的打印开发中经常需要自定义纸张,具体步骤如下 ...
- SAP内表转XML文件
今天有个兄弟问如何实现以XML的方式输出内表的内容,这个问题我以前好像没有写过.倒不是不会写,而是写的方法太多了,有极其简单的,也有很复杂的,而且网上资料也很多. 找到以前写的一个程序,稍微修改了一下 ...