计算组合数C(n,m)(SDUT 2241)

计算组合数C(n,m)

          Time Limit: 1000MS Memory Limit: 65536KB

Problem Description
C(n,m)=n!/(m! * (n-m)!).(0<=n,m<=10^8且m<=n,该题结果保证在int范围之内)。
Input

第一行是一个正整数t,下面t行每行有两个整数n和m。
Output

对于每一行输入,分别对应输出组合数C(n,m)的值
Example Input

3
1 1
2 1
3 2
Example Output

1
2
3
Hint

题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2241.html

这道题很水,但是因为好长时间没有做题了,觉得有个思想还不错。
如果做这道题是硬计算阶乘,那么即使用long long也存不下,这时就可以将数学公式简化。比如:要计算n!/m!,其实就相当于计算n*(n-1)(n-2)(n-3)……*(m+1),因此就简化了计算。

AC的代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
#define N 100010
int main(void)
{
    int t;
    scanf("%d",&t);
    double n,m;
    double i;
    double res;
    while(t--){
        scanf("%lf%lf",&n,&m);
        res = 1;
        for(i = n; i > m; i--)
            res *= i;
        for(i = n-m; i >1; i--)
            res /= i;
        printf("%.0lf\n", res);
    }
    return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值