poj1942 Paths on a Grid
处理阶乘有三种办法:
(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),非常可观。
---------------------以上转自小优的博客-----------------------------------------------------
我只知道第一种方法加c(m,n)=c(m-1,n)+c(m-1,n-1)这个。显然根本不知道还可以用第三种方法double这么做,我以为,,,我以为
还要注意看是数据是unsigned的类型
#include <stdio.h>
unsigned find(unsigned a,unsigned b){
double res=;
b=b<(a-b)?b:(a-b);
while(b){
res*=(double)(a--)/(double)(b--);
}
res+=0.5;
return (unsigned)res;
}
int main(){
unsigned m,n;
while(~scanf("%u%u",&m,&n)){
if(m==&&n==) break;
printf("%u\n",find(m+n,m));
}
return ;
}
poj1942 Paths on a Grid的更多相关文章
- poj1942 Paths on a Grid(无mod大组合数)
poj1942 Paths on a Grid 题意:给定一个长m高n$(n,m \in unsigned 32-bit)$的矩形,问有几种走法.$n=m=0$时终止. 显然的$C(m+n,n)$ 但 ...
- POJ1942——Paths on a Grid(组合数学)
Paths on a Grid DescriptionImagine you are attending your math lesson at school. Once again, you are ...
- POJ1942 Paths on a Grid(组合)
题目链接. 分析: #include <cstdio> #include <iostream> #include <map> #include <cstrin ...
- Paths on a Grid(简单组合数学)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...
- Paths on a Grid(规律)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23270 Accepted: 5735 ...
- [ACM] POJ 1942 Paths on a Grid (组合)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21297 Accepted: 5212 ...
- POJ 1942:Paths on a Grid
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22918 Accepted: 5651 ...
- POJ - 1942 D - Paths on a Grid
Imagine you are attending your math lesson at school. Once again, you are bored because your teacher ...
- Paths on a Grid(poj 1942)
给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有多少种走法,每步只能向上或者向右走. //注意循环的时候,要循环小的数,否 ...
随机推荐
- 从源代码制作deb包的两种方法以及修改已有deb包(转载)
From:http://yysfire.github.io/linux/%E4%BB%8E%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%B6%E4%BD%9Cdeb%E5%8C% ...
- SQL Server查询死锁并KILL
杀掉死锁的sqlserver进程 SELECT request_session_id spid,OBJECT_NAME (resource_associated_entity_id)tableNa ...
- java多线程的使用2
1.join与interrupt的用法 class Sleeper extends Thread { private int duration; public Sleeper(String name, ...
- 配置HylaFAX传真服务器
配置HylaFAX传真服务器转自 http://blog.chinaunix.net/uid-8551991-id-248081.html参考:http://www.hylafax.org/howto ...
- delphi 程序全屏显示无标题栏,覆盖整个屏幕
delphi 程序全屏显示无标题栏,覆盖整个屏幕,这个在做工控机或屏保时有用的,所以记下 procedure TMainFrm.FormCreate(Sender: TObject); begin w ...
- SQL备份表及相关笔记
create table history1301( remark nvarchar(64))create table history1302( remark nvarchar(64))create t ...
- ListView之setEmptyView的问题
使用listView或者gridView时,当列表为空时,有时需要显示一个特殊的empty view来提示用户,一般情况下,如果你是继承ListActivity,只要 <ListView and ...
- struts2+hibernate+poi导出Excel实例
本实例通过struts2+hibernate+poi实现导出数据导入到Excel的功能 用到的jar包: poi 下载地址:http://poi.apache.org/ 根据查询条件的选择显示相应数据 ...
- Oracle 的过程与函数
一.过程 1 .过程创建和调用 过程 (procedure) 是一个 PL/SQL 语句块,它存储在数据字典中并可被应用程序调用.可以使用过程存储数据库中频繁使用的应用逻辑.当执行一个过程时,其语句被 ...
- Hadoop JobHistory
hadoop jobhistory记录下已运行完的MapReduce作业信息并存放在指定的HDFS目录下,默认情况下是没有启动的,需要配置完后手工启动服务. mapred-site.xml添加如下配置 ...