Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) F

题意:

Tarly有f个食物箱和w个酒桶,现在他想要将它们存在一些栈里,然后将这些栈排成一行,要求满足任意相邻的两个栈存放的物品不同

而Jon Snow不喜欢每个存酒的栈里酒的数量小于等于h,问在满足以上限制的前提下出现可行解的概率为多少?(假设每种满足Tarly要求的摆放方案出现的概率均等)

0 <= f,w,h <= 1E5 答案mod 1E9 + 7输出

solution:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 2E5 + 20;
typedef long long LL;
const LL mo = 1000000007;

int f,w,h,t,Ans,tot,Fac[maxn],Inv[maxn];

int Mul(const LL &x,const LL &y) {return x * y % mo;}
int Add(const int &x,const int &y) {return (x + y) % mo;}

int ksm(int x,int y)
{
	int ret = 1;
	for (; y; y >>= 1)
	{
		if (y & 1) ret = Mul(ret,x);
		x = Mul(x,x);
	}
	return ret;
}

int C(int N,int M)
{
	return Mul(Fac[N],Mul(Inv[M],Inv[N - M]));
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> f >> w >> h; Fac[0] = 1;
	if (!w) {cout << 1 << endl; return 0;}
	++h; t = w / h;
	if (w < h) {cout << 0 << endl; return 0;}
	for (int i = 1; i <= f + w; i++) Fac[i] = Mul(Fac[i - 1],i);
	Inv[f + w] = ksm(Fac[f + w],mo - 2);
	for (int i = f + w - 1; i >= 0; i--) Inv[i] = Mul(Inv[i + 1],i + 1);
	tot = ksm(C(f + w,f),mo - 2);
	for (int i = 1; i <= min(f + 1,t); i++)
		Ans = Add(Ans,Mul(C(f + 1,i),C(w - i * h + i - 1,i - 1)));
	cout << Mul(Ans,tot) << endl;
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值