[ACM] POJ 1942 Paths on a Grid (组合)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 21297 | Accepted: 5212 |
Description
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
Output
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
Source
代码:
#include <iostream>
#include <algorithm>
using namespace std; long long c(long long n,long long m)
{
long long ans=1;
for(int i=1;i<=m;i++)
ans=ans*(n--)/i;
return ans;
} int main()
{
long long n,m;
while(cin>>n>>m&&(n||m))
{
if(n<m)
swap(n,m);
cout<<c(n+m,m)<<endl;
}
return 0;
}
http://blog.csdn.net/lyy289065406/article/details/6648516在里面学到了两种新的求解组合数的方法。
处理阶乘有三种办法:
(1) 传统意义上的直接递归。n的规模最多到20+,太小了,在本题不适用,并且很慢
(2) 稍快一点的算法,就是利用log()化乘为加。n的规模尽管扩展到1000+,可是因为要用三重循环,一旦n规模变得更大。耗时就会很之严重,时间复杂度达到O(n*m*(n-m)),本题规定了n,m用unsigned int32类型,就是说n,m的规模达到了21E以上。铁定TLE的。
并且就算抛开时间不算,还存在一个致命的问题,就是精度损失随着n的添加会变得很严重。
由于n有多大。就要进行n次对数运算。n规模一旦过大,就会丢失得很严重了。所以这样的方法是绝对不可取的,由于中途的精度丢失不是简单的四舍五入能够挽回的。
(3) 拆分阶乘。逐项相除,再乘曾经面全部项之积。
这样的方法用一个循环就OK了。时间复杂度仅仅有O(n-m),很可观。
以下我依据程序具体说说算法(3):
double cnm=1.0;
while(b>0)
cnm*=(double)(a- -)/(double)(b- -);
这是我写的函数原型。计算的是 aCb
这样的算法巧妙地利用了分子分母的关系,而不是把公示中的3个阶乘单独处理。
比如当 a=5,b=2时

因为用了 double去计算组合数。那么最后要转化为 无符号整型 时就要处理精度问题,有两种方法:四舍五入+强制类型转换 或者 用 setprecision()函数
- unsigned comp(unsigned n,unsigned m)
- {
- unsigned a=m+n;
- unsigned b=(m<n?m:n);
- double cnm=1.0;
- while(b>0)
- cnm*=(double)(a--)/(double)(b--);
- cnm+=0.5; //double转unsigned会强制截断小数。必须先四舍五入
- return (unsigned)cnm;
- }
- double comp(unsigned n,unsigned m)
- {
- unsigned a=m+n;
- unsigned b=(m<n?m:n);
- double cnm=1.0;
- while(b>0)
- cnm*=(double)(a--)/(double)(b--);
- return cnm;
- }
- cout<<fixed<<setprecision(0)<<comp(n,m)<<endl;
- //fixed是为了固定小数位数
- //setprecision()函数是会自己主动四舍五入的,所以不用像强制类型转换那样预先+0.5
[ACM] POJ 1942 Paths on a Grid (组合)的更多相关文章
- POJ 1942 Paths on a Grid(组合数)
http://poj.org/problem?id=1942 题意 :在一个n*m的矩形上有n*m个网格,从左下角的网格划到右上角的网格,沿着边画,只能向上或向右走,问有多少条不重复的路 . 思路 : ...
- POJ 1942 Paths on a Grid
// n*m 的格子 从左下角走到右上角的种数// 相当于从 n+m 的步数中选 m 步往上走// C(n+m,m) #include <iostream> #include <st ...
- poj 1924 Paths on a Grid(组合数学)
题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...
- POJ1942 Paths on a Grid(组合)
题目链接. 分析: #include <cstdio> #include <iostream> #include <map> #include <cstrin ...
- POJ 1942:Paths on a Grid
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22918 Accepted: 5651 ...
- 北大ACM - POJ试题分类(转自EXP)
北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...
- Paths on a Grid(简单组合数学)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...
- POJ1942——Paths on a Grid(组合数学)
Paths on a Grid DescriptionImagine you are attending your math lesson at school. Once again, you are ...
- Paths on a Grid(规律)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23270 Accepted: 5735 ...
随机推荐
- JavaScript入门二
******函数****** **函数定义** //普通函数定义 function f1() { console.log("Hello word!") } //带参数的函数 fun ...
- ACM_黑色星期五
黑色星期五 Time Limit: 2000/1000ms (Java/Others) Problem Description: 13号又是星期五是一个不寻常的日子吗?13号在星期五比在其他日少吗?为 ...
- Spring思维课程导图——bean得实例化和bean的管理
- C#入门经典 Chapter4 流程控制
4.1布尔逻辑 布尔比较运算符 == != < > <= >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...
- 《Java编程的逻辑》第一部分 编程基础与二进制
- CSS——img标签消除3px
1.dispaly:block 2.float:left 这两种都可以消除3px
- /etc目录常用配置文件
/etc/resolv.conf DNS客户端配置文件,逐渐被网卡配置文件所替代 /etc/hosts 本机DNS解析文件,优先级高于DNS服务器 /etc/hostname CentOS 7 主机名 ...
- 如何让 HTML 识别 string 里的 '\n' 并成功换行
只要在结果所在的 div 的 css 设置: white-space: pre-line; 然后页面就能成功识别 '\n' 并整齐的显示结果了.
- HDU 1465(错排公式)
不容易系列之一 题意: 一个人要寄n个信封,结果装错了.信纸的编号为1到n,信封的编号为1到n,信纸的编号不能和信封的编号一样,全都不能一样. 思路:错排公式. D(n)表示n件信封装错的所有的情况. ...
- centOS防火墙
默认防火墙firewall #停止firewall systemcl stop firewall.service #禁止firewall开机启动 systemctl disable firewall. ...