Light OJ 1102 Problem Makes Problem 组合数

题目来源:Light OJ 1102 Problem Makes Problem

题意:一个整数n分解成k个数相加有多少种方案 数字可以重复 

思路:m个苹果放n个盒子有多少方案 允许盒子有空 盒子空对应0 答案是C(n+m-1, n-1) 

首先如果不允许有空 答案是C(m-1, n-1) 插空法m个苹果有m-1个空档 在这m-1个空档里选n-1个分成了n部分 每部分都不是空的

现在允许为空 那么给每个盒子增加一个苹果 相当于n+m个苹果分成n份 n+m-1个空档里选n-1个  C(n+m-1, n-1) 

每部分都至少为1 为1的其实是空的 是我开始加上去的 为的是方便用插空法 


#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL mod = 1000000007;
const int maxn = 2000010;
//返回a^p mod n 快速幂
LL a[maxn];
LL pow_mod(LL a, LL p, LL n)
{
	LL ans = 1;
	while(p)
	{
		if(p&1)
		{
			ans *= a;
			ans %= n;
		}
		a *= a;
		a %= n;
		p >>= 1;
	}
	return ans;
}

LL C(LL n, LL m)
{
	LL x = a[n], y = a[n-m]*a[m]%mod;
	return x*pow_mod(y, mod-2, mod)%mod;
}
int main()
{
	a[0] = 1;
	for(int i = 1; i <= 2000000; i++)
	{
		a[i] = a[i-1]*i;
		a[i] %= mod;
	}	
	int cas = 0; 
	int T;
	scanf("%d", &T); 
	while(T--)
	{
		LL n, m;
		scanf("%lld %lld", &n, &m);
		printf("Case %d: %lld\n", ++cas, C(n+m-1, m-1));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值