• Paths on a Grid POJ 1942 (组合数学 || 暴力)


     
    Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time he's explaining that (a+b) 2=a 2+2ab+b 2). So you decide to waste your time with drawing modern art instead.

    Fortunately you have a piece of squared paper and you choose a rectangle of size n*m on the paper. Let's call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:

    Really a masterpiece, isn't it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?
    Input
    The input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimension. Input is terminated by n=m=0.
    Output
    For each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how many paths are there on a grid where each step of the path consists of moving one unit to the right or one unit up? You may safely assume that this number fits into a 32-bit unsigned integer.
    Sample Input
    5 4
    1 1
    0 0
    
    Sample Output
    126
    2
    题意分析:这道题目是说在n*m 的方格中找到一条路,使从最左上角到最右下角(这里从左下到右上没有区别,这里暂时就这样理解),也就是我们肯定要往右走,对我来说选择就是往右或者往下,但是路的总长度不会变(m+n),那就可以用到排列组
    合,C((m+n),n),这里如果直接算阶乘那数据肯定会超int,所以考虑约分C((m+n),n) = (n+m)!/(n!*m!) = ((n+1)*(n+2)...(m+n))/(m!);
    这种方法的出处:https://blog.csdn.net/were__wolf/article/details/12002359
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <queue>
    #include <stack>
    #include <math.h>
    #include <ctype.h>
    #include <iostream>
    using namespace std;
    #define LL unsigned long long
    int main()
    {
        LL m,n;
        while(scanf("%lld %lld", &m, &n),m+n)
        {
            LL sum = m+n;
            if(m > n)
                swap(n,m);
            double s = 1;///这个地方刚开始不理解为什么是double类型 无符号long long
            while(m > 0)///就是不行,然后问了一下学长 学长给了一个极其暴力的方法╮( ̄▽ ̄")╭
            {
                s = s * sum / m;
                m--;
                sum--;
            }
            printf("%.0f
    ",s);
        }
        return 0;
    }

    首先知道题目说输出结果在int范围内,然后测试数据当二维数组开到30*30是,int就存不下了,这个时候来三个特判,剩下的就不会超int按照正常二维数组打表可以搞定.

    学长的暴力真的是不讲道理啊!!!

  • 相关阅读:
    HashMap源码解析
    如何同时运行两个Tomcat
    Thymeleaf遍历List<Map>和Map<>
    Apache POI详解
    如何管理系统的右键新建菜单
    datatable的dom配置
    如何获取Html的height和width属性(网页宽、高)
    什么是跨域?如何解决跨域问题?
    C# 各类文件扩展名
    C# 接口与抽象类的区别? 什么情景下使用接口,什么情景下使用抽象类?
  • 原文地址:https://www.cnblogs.com/mashen/p/9026403.html
Copyright © 2020-2023  润新知