简单组合数学

题目:
To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way:

Initially, all lights on William’s device are turned off. At the beginning of a new iteration the device randomly, with a uniform distribution, picks a light that is turned off and turns it on, telling William which cryptocurrency he should invest in. After this iteration if any k consecutive lights contain more than one turned on light, then the device finishes working.

William doesn’t like uncertainty, so he wants you to calculate the expected value of the number of lights that are turned on in the device after it finishes working.

题目分析:
很简单的组合数学问题,捆绑分析一下,不难发现答案为:
a n s = ( a n s + C ( n − ( k − 1 ) ∗ ( i − 1 ) , i ) ∗ i n v ( C ( n , i ) , m o d ) ) ans=(ans+C(n-(k-1)*(i-1),i)*inv(C(n,i),mod)) ans=(ans+C(n(k1)(i1),i)inv(C(n,i),mod))

具体细节见代码:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
	T f=1;x=0;
	char ch=getchar();
	while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	x*=f;
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
const int mod=1e9+7;
LL fac[N],inv[N];
LL q_pow(LL a,LL b) {
	LL ans=1;
	while(b) {
		if(b&1) {
			ans=ans*a%mod;
		}
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
void init() {
	fac[0]=1;
	for(int i=1;i<N;i++) {
		fac[i]=fac[i-1]*i%mod;
	}
	inv[N-1]=q_pow(fac[N-1],mod-2);
	for(int i=N-2;i>=0;i--) {
		inv[i]=inv[i+1]*(i+1)%mod;
	}
}
LL C(int n,int m) {
	return fac[n]*inv[n-m]%mod*inv[m]%mod;
}
int main()
{
	init();
	int w;
	cin>>w;
	while(w--) {
		int n,k;
		read(n),read(k);
		LL ans=1;
		for(int i=1;n-(k-1)*(i-1)>=i;i++) {
			ans=(ans+C(n-(k-1)*(i-1),i)*q_pow(C(n,i),mod-2))%mod;
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值