一些项目——Delta-wave

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

代码

#include<stdio.h>
#include<math.h>
void find(int n,int &l,int &r,int &level)
{
    int i;
    level=1;
    for(i=1;;i+=2)
    {
        if(n-i<=0)
        {
            l=(n+1)/2;
            r=(i-n)/2+1;
            break;
        }
        level++;
        n-=i;
    }
}
int main()
{
    int m,n;
    int ml,mr,nl,nr,mlevel,nlevel;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        find(m,ml,mr,mlevel);
        find(n,nl,nr,nlevel);
        printf("%d\n",abs(ml-nl)+abs(mr-nr)+abs(mlevel-nlevel));
    }
    return 0;
}
时间: 2024-08-17 07:20:56

一些项目——Delta-wave的相关文章

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

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

UVA - 1478 Delta Wave (大数+卡特兰数)

Description A delta wave is a high amplitude brain wave in humans with a frequency of 1 - 4 hertz which can be recorded with an electroencephalogram (EEG) and is usually associated with slow-wave sleep (SWS). - from Wikipedia The researchers have dis

HDU 3723 Delta Wave(默慈金数)

传送门 Delta Wave Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1160    Accepted Submission(s): 370 Problem Description A delta wave is a high amplitude brain wave in humans with a frequency of 1

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

题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答案应该是sum(C(n, 2*i)*C(n, i)/(i+1)) = a1 + a2 + a3 + ....,然后可以再化简一下,成为一个递推式ai = ai-1 * (n-2*i+1)*(n-2*i+2)/(k*(k+1)): 这次用记事本写的,然后没有测试,直接交的,虽然CE了一发,但还是挺好的

LA 5092 &amp;&amp; hdu 3723 Delta Wave (卡特兰数)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3723 and http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20568 题意:有种折线每向右延伸一个单位长度,高度要么不变,要么加1,要么减1.而且任何时刻高度不能低于0.求这种折线最终高度为0的情况总数. 分析:由于任何时刻斜向上的线不小于斜向下的线的数目,而且最终相等,,,,,卡特兰数模型.卡特兰数资料 若有i条斜向上的线,那

UVa 1478 - Delta Wave

题目:求从(0,0)到(N,0)的路径数,每次可以斜向上或者斜向下或者直走.(不能走到负区域) 分析:组合,计数,卡塔兰数,大整数. 因为不能走到负的区域,所以上升的和下降的此时必然相等,而且上升次数要随时不小于下降次数. 由此可知,上升和下降是上面提到的括号合法匹配,枚举所有的上升下降次数有: 边长为n的图中走法数 F(n)= Σ(C(n,2*i)*Ci),其中: C(n,2*i)是n条路中有i条上升和i条下降的方案数,Ci是i条上升和i条下降的合法组合数(内部). 化简:F(n)= Σ(C(

HDU3723-Delta Wave(Catalan数+组合计数)

Delta Wave Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 741    Accepted Submission(s): 243 Problem Description A delta wave is a high amplitude brain wave in humans with a frequency of 1 – 4

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

Nagios+NCPA环境搭建

NCPA 插件监控原理  1. Nagios server 1.1 安装nagios前提条件 yum install -y wget httpd php gcc glibc glibc-common gd gd-devel make net-snmp curl  # nagios用的到的gcc编译器\http等支持软件 1.2 下载nagios core与 plugin curl -C - -O https://assets.nagios.com/downloads/nagioscore/rel

造一个Badge Service的轮子

什么是Badge Service 细心的读者朋友一定在很多Github的Repo,npm的package页面看到过诸如 的徽章.这些徽章是干什么用的? 大家看到上文中我引用的Badge的左侧,是Downloads,Build,Dependency之类的,不难猜出,这些Badge是用于一些信息的统计,然后以图片的形式展示出来.比如我的一个小项目Delta是用js写的,我使用了Travis CI来进行持续的编译和测试.每次我checkin了新的代码,Travis CI就会对最新的代码进行build,