您的位置:首页 > 其它

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

2017-02-22 19:40 615 查看
题意:

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
,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐