Delta-wave

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

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
 
思路:数学题,把每个数字对应的行号(1,2,3。。。。。)算出来,行号h = sqrt(n)为整数则不加1,否则加1,得到的就是行号,再分别算出从左边和从右边起的列号(1,2,3。。。),右列号rl = (h*h-n)/2+1,左列号ll = (n-((h-1)*(h-1)+1))/2+1,然后两个数对应的行,列号相减的绝对值之和就是最短路线。
 
 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    int n,m,h_n,h_m,ll_n,rl_n,rl_m,ll_m,r;
    while(~scanf("%d%d",&n,&m))
    {
        if(sqrt(n)>(int)sqrt(n))
           h_n = (int)sqrt(n)+1;
        else
           h_n = (int)sqrt(n);
        rl_n = (h_n*h_n-n)/2+1;
        ll_n = (n-((h_n-1)*(h_n-1)+1))/2+1;
         if(sqrt(m)>(int)sqrt(m))
           h_m = (int)sqrt(m)+1;
        else
           h_m = (int)sqrt(m);
        rl_m = (h_m*h_m-m)/2+1;
        ll_m = (m-((h_m-1)*(h_m-1)+1))/2+1;
        r = abs(h_n-h_m)+abs(ll_n-ll_m)+abs(rl_n-rl_m);
        printf("%d\n",r);
    }
    return 0;
}

 

Delta-wave的更多相关文章

  1. uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

    option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...

  2. HDU3723 Delta Wave —— 卡特兰数

    题目链接:https://vjudge.net/problem/HDU-3723 Delta Wave Time Limit: 6000/3000 MS (Java/Others)    Memory ...

  3. HDU 3723 Delta Wave (高精度+calelan数)

    题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答 ...

  4. RIFF和WAVE音频文件格式

    RIFF file format RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构.RIFF文 ...

  5. IEEE 802.11p (WAVE,Wireless Access in the Vehicular Environment)

    IEEE 802.11p(又称WAVE,Wireless Access in the Vehicular Environment)是一个由IEEE 802.11标准扩充的通讯协定.这个通讯协定主要用在 ...

  6. Wave - 花たん 音乐

    Wave 歌手:花たん 所属专辑:Flower 間違えて宇宙終わって(宇宙因为一个错误而终结了) 青信号はいつも通り(通行的灯号一如往常的) 飛んでまた止まって(又再停止传播) また 飛びそうだ(然后 ...

  7. Dirac Delta Function

    也称为Degenerate pdf, 退化概率密度函数. 未经考证的解释是: 当正态分布的\(\sigma \to 0\)时, 正态分布就退化为这个分布了. 定义 \[ \delta(x) = \be ...

  8. Web 播放声音(AMR 、WAVE)

    最近甚是苦闷,属于边学边做,跳进了很多坑,别提有多惨了,不过结果还是不错滴,纵观前后,一句话足以概括 “痛并快乐着” ~~~ ok,我少说废话,下面来总结下 Web 播放声音一些注意事项. 说到 We ...

  9. 数据源增量处理(Delta Proess)三大属性:Recod Mode、Delta Type、Serialization

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. 对冲的艺术——delta中性交易

    delta中性交易 delta中性交易——外行话 delta中性交易就是构造一个含有期权头寸的组合,使其不受标的股票或指数价格小幅变动的影响.换句话讲,无论标的价格是涨还是跌,组合的市值始终保持不变. ...

随机推荐

  1. javascript DOM 节点 第18节

    <html> <head> <title>DOM对象</title> </head><body><div >DOM对 ...

  2. greenlet代码解读

    协程 上次已经讲解了协程的的实现方法,和我对协程的一些理解.这里指我就先以代码说明协程的运行.def test1():    print 12         (2)    gr2.switch()  ...

  3. sed工具使用

    sed命令使用形式 1.sed命令从管道中读取数据处理 command | sed ' edit command' 通过管道把一个命令的标准输出读入到sed的标准输入,sed就起到了过滤作用 2.se ...

  4. apache .htaccess 伪静态重定向,防盗链 限制下载...

    301全站跳转 RewriteEngine OnRewriteCond %{HTTP_HOST} ^www\.old\.net$ [NC]RewriteRule ^(.*)$ http://www.n ...

  5. SQL 添加字段和默认值脚本

    --插入字段和默认值alter table Acc_WayBill add DeclaredValue nvarchar(50)goEXEC sys.sp_addextendedproperty @n ...

  6. 对React的理解

    转自:http://www.cocoachina.com/webapp/20150721/12692.html 现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了R ...

  7. 文字以及div水平垂直居中

    文字以及div水平垂直居中.md <div class=”content”> <div class=”mydiv”> huangyingnin! </div>< ...

  8. js与uri中location关系

    //获取域名host = window.location.host;host2=document.domain; //获取页面完整地址url = window.location.href; docum ...

  9. C# 程序关闭和进程关闭

    this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit();  强制所有消息中止, ...

  10. URL传参中不能带特殊的字符以及处理方案

    有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.编码的格式为:%加字符的ASCII码,即一个百分号%,后面跟对应字符的ASCII(16进制)码值.例如 ...