题目链接:http://poj.org/problem?id=1942

题意:实际上这道题就是求C(n+m,n)。

思路:n、m的范围在unsigned中,所以不能递推计算组合数,可以采用公式C(a,b)=a!/(b!*(a-b)!),并且拆分阶乘依次进行除法运算。我在一个地方TLE了半小时,开始怎么也想不通怎么会T,后来发现我的getc函数的形参传递的是int,这样一旦实参是int所不能表示时,传递过来的可能是负数,那么在while循环里就会出现死循环,所以就T了。写代码真的要细心一些啊QAQ。还有一个细节是输入可能出现X 0 (X>0),应该输出1,而不是结束。

AC代码:

 #include<cstdio>
#include<algorithm>
using namespace std; unsigned n,m; unsigned getc(unsigned x,unsigned y){
unsigned a=x+y,b=min(x,y);
double ans=1.0;
while(b>)
ans*=(double)(a--)/(double)(b--);
ans+=0.5;
return (unsigned)ans;
} int main(){
while(~scanf("%u%u",&n,&m),n||m)
printf("%u\n",getc(n,m));
return ;
}

poj1942(求组合数)的更多相关文章

  1. lucas求组合数C(n,k)%p

    Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...

  2. URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理

    URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...

  3. N!分解质因子p的个数_快速求组合数C(n,m)

    int f(int n,int p) { ) ; return f(n/p,p) + n/p; } https://www.xuebuyuan.com/2867209.html 求组合数C(n,m)( ...

  4. 求组合数、求逆元、求阶乘 O(n)

    在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...

  5. HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解

    题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...

  6. hdu 2519 求组合数

    求组合数 如果求C5 3 就是5*4*3/3*2*1 也就是(5/3)*(4/2)*(3/1) Sample Input5 //T3 2 //C3 25 34 43 68 0 Sample Outpu ...

  7. 求组合数 C++程序

    一 递归求组合数 设函数为void    comb(int m,int k)为找出从自然数1.2.... .m中任取k个数的所有组合. 分析:当组合的第一个数字选定时,其后的数字是从余下的m-1个数中 ...

  8. HDU 5698——瞬间移动——————【逆元求组合数】

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  9. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  10. 51nod1119(除法取模/费马小定理求组合数)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 题意:中文题诶- 思路:这题数据比较大直接暴力肯定是不 ...

随机推荐

  1. canal 配置

    参考:https://www.2cto.com/database/201609/547661.html Spring配置 spring配置的原理是将整个配置抽象为两部分: xxxx-instance. ...

  2. js判断用户是客户端还是移动端

    js判断用户是客户端还是移动端 Javascript 判断客户端是否为 PC 还是手持设备,有时候项目中需要用到,很方便的源生检测,方法一共有两种   1.第一种: function IsPC() { ...

  3. Mac上python2和python3的版本切换

    在命令行执行 vi ~/.bash_profile 在文件下面加上: alias python2='/system/Library/Frameworks/Python.framework/Versio ...

  4. Oracle函数使用

    数据格式化截取:  Trunc(data,[yyyy]) oracle的特有if判断: decode(sex, 0, '男', 1, '女') 分组排序:row_number() over(parti ...

  5. js弹出div层,弹出层页面底部出现UL出现一条线问题

    整个弹出div层,列表满一页时:底部会出现一条横线 原因:ul固定写在页面中了 解决方法: 将ul代码与li列表一样写在js中,如下 var newhtml = '<ul class=" ...

  6. THE BOX MODEL

    Review In this lesson, we covered the four properties of the box model: height and width, padding, b ...

  7. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  8. TypeScript语言学习笔记(1)

    基本类型 // 布尔型(Boolean) let isDone: boolean = false; // 数值型(Number) let decimal: number = 6; let hex: n ...

  9. python的解构

    今天学习python看到python的解构,觉得很有用就写下来,防止自己忘了 首先定义个列表 然后我们来解构 字典呢?字典需要两个*号才能解构 这样调用不明显 来个明显点的 上面错误是,你定义了一个形 ...

  10. Python中os模块使用方法

    os模块提供了对系统环境.文件.目录等操作系统级的接口函数.本文主要描述os模块和os.path模块常用函数以及常用实例. os模块函数 os.getcwd() 获取当前工作的目录. os.listd ...