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)的方法数,例如数字 ...
随机推荐
- a byte of vim -- 学习摘要
说在前面的话 -- a byte of vim 是我见过的最介绍vim 最好的书,想了解强大的vim的人,或者是已经在使用vim而打算进一步了解的人,我感觉都应该看看这个,内容精炼但涵盖非常广,--& ...
- RabbitMQ与Redis做队列比较
本文仅针对RabbitMQ与Redis做队列应用时的情况进行对比 具体采用什么方式实现,还需要取决于系统的实际需求简要介绍RabbitMQRabbitMQ是实现AMQP(高级消息队列协议)的消息中间件 ...
- eclipse 创建maven web 项目
虽然网上教程一大把,但也重新整理下. 一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写 ...
- MySql存储过程及MySql常用流程控制语法
/* 该代码是创建了一个名叫"p4"的存储过程并设置了s1,s2,s3两个int型一个varchar型参数,还可以是其他数据类型,内部创建了x1,x2两个变量 DELIMITER是 ...
- cocos2dx的ui封装
cocos2dx里加载cocosudio导出的ui配置文件,在这之上封装了一下,封装核心类包括 UIManager,UILayer,UIOwner UIManager是所有ui总的管理类,代码如下: ...
- Django之restframework2视图三部曲
视图三部曲 下面我来来看restframework是如何将冗余的代码一步步的进行封装. 这里主要用到的是多继承 第一步mixin类编写视图 AuthorModelSerializer: class A ...
- Django之表单验证
对于前端的表单进行验证的方法,从最简单的js到基于XML传输的Ajax,再到cookie的免认证,现在Django为我们提供了自带的表单验证方法. views.py: from django impo ...
- iOS8 with Swift
Ref:iOS8 Day-by-Day Ref:iOS8-day-by-day source Ref:Let's Swift Ref:Swift 代码库 Ref:iOS Apprentice Thir ...
- Java基础教程:反射基础
Java基础教程:反射基础 引入反射 反射是什么 能够动态分析类能力的程序称为反射. 反射是一种很强大且复杂的机制. Class类 在程序运行期间,Java运行时系统始终为所有对象维护一个被称为运行时 ...
- Data Structure Linked List: Detect and Remove Loop in a Linked List
http://www.geeksforgeeks.org/detect-and-remove-loop-in-a-linked-list/ #include <iostream> #inc ...