SDNU 1245.这题超难的,建议先做其他的,看都不要看(思维)

本文介绍了一种优化方法,用于高效计算组合数C(n,m),避免了传统方法中可能遇到的溢出问题。通过实例演示了优化算法的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

求组合数C(n,m)

Input

T(0 < T < 2000)
每组样例两个数n, m(0 < n, m< 60)
 

Output

C(n,m)

Sample Input

2
1 1
2 1

Sample Output

1
2

Source

Unknown
思路:真是一道神奇的题啊,我想着用常规的办法去求解,但是爆了long long也不自知,真是傻。然后用了优化。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long

int t, n, m;
ll sum1;

int main()
{
    scanf("%d", &t);
    while(t--)
    {
        sum1 = 1;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; i++)
        {
            sum1 *= (n-i+1);
            sum1 /= i;
        }
        printf("%lld\n", sum1);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/10991748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值