Codeforces 932E Team work 【组合计数+斯特林数】

15 篇文章 0 订阅
6 篇文章 0 订阅

Codeforces 932E Team work


You have a team of N people. For a particular task, you can pick any non-empty subset of people. The cost of having x people for the task is xk.

Output the sum of costs over all non-empty subsets of people.

Input

Only line of input contains two integers N (1 ≤ N ≤ 109) representing total number of people and k (1 ≤ k ≤ 5000).

Output

Output the sum of costs for all non empty subsets modulo 109 + 7.

Examples

input

1 1

output

1

input

3 2

output

24

Note

In the first example, there is only one non-empty subset {1} with cost 11 = 1.
In the second example, there are seven non-empty subsets.
- {1} with cost 12 = 1
- {2} with cost 12 = 1
- {1, 2} with cost 22 = 4
- {3} with cost 12 = 1
- {1, 3} with cost 22 = 4
- {2, 3} with cost 22 = 4
- {1, 2, 3} with cost 32 = 9
The total cost is 1 + 1 + 4 + 1 + 4 + 4 + 9 = 24.


ans=ni=1Cinik a n s = ∑ i = 1 n C n i ∗ i k
ik i k 用斯特林数展开
ans=ni=1Cinij=1SjkCjij! a n s = ∑ i = 1 n C n i ∑ j = 1 i S k j ∗ C i j ∗ j !
换一下位置
ans=kj=1Sjkj!ni=1CinCji a n s = ∑ j = 1 k S k j ∗ j ! ∑ i = 1 n C n i ∗ C i j
又因为 ni=1CinCji=Cjn2nj ∑ i = 1 n C n i ∗ C i j = C n j ∗ 2 n − j
所以 ans=kj=1Sjkj!Cjn2nj a n s = ∑ j = 1 k S k j ∗ j ! ∗ C n j ∗ 2 n − j

组合数可以O(k)预处理,所以总时间 O(k2+klogn) O ( k 2 + k l o g n )

#include<bits/stdc++.h>
using namespace std;
#define N 5010
#define yyf 1000000007
#define LL long long
LL S[N][N],inv[N],C[N],J[N];
LL n,k;
LL fast_pow(LL a,LL b){
    LL ans=1;
    while(b){
        if(b&1)ans=ans*a%yyf;
        b>>=1;
        a=a*a%yyf;
    }
    return ans;
}
int main(){
    cin>>n>>k;
    inv[0]=inv[1]=1;C[1]=n;J[1]=1;
    for(LL i=2;i<=k;i++)J[i]=J[i-1]*i%yyf;
    for(LL i=2;i<=k;i++)inv[i]=(yyf-yyf/i)*inv[yyf%i]%yyf;
    for(LL i=2;i<=k;i++)C[i]=C[i-1]*inv[i]%yyf*(n-i+1)%yyf;
    S[0][0]=1;
    for(LL i=1;i<=k;i++){
        S[i][0]=0;
        for(LL j=1;j<=i;j++)S[i][j]=(j*S[i-1][j]%yyf+S[i-1][j-1])%yyf;
    }
    LL ans=0;
    for(LL i=1;i<=min(k,n);i++)ans=(ans+S[k][i]*J[i]%yyf*C[i]%yyf*fast_pow(2,n-i)%yyf)%yyf;
    printf("%lld",ans%yyf);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值