题目:
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−(k−1)∗(i−1),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;
}