E - Team Work CodeForces - 932E 组合数学求导 dp

62 篇文章 0 订阅
22 篇文章 0 订阅

E. Team Work

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.


 

题意:

      你可以在n个人中选出非空的i个人子集,这个子集的权值为i的k次,问这n个人可以得到的权值为多少。

 

做法:

      很明显求的是   \sum _{i=1}^{n}{C_{n}}^{i}*i^{k}  ,但是由于n很大,直接暴力肯定是T的,注意到k的范围只有5000,所以可以用dp来做。利用(1+X)^{n}二项式定理展开(1+x)^{n}=\sum C_{n}^{i}*x^{i} 进行k次求导,每次求导后再次乘上x,x次求导之后把x变为1则可获得想求的值,居然见代码


代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll n,k,ans,dp[5500][5500];//dp[i][j]表示第i次求导后x为j时的系数
ll quick(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b/=2;
    }
    return ans;
}
int main(){
    ans=0;
    scanf("%lld%lld",&n,&k);
    dp[1][1]=n;//第一次求导得到一项,系数为n
    for(int i=1;i<k;i++){
        for(int j=1;j<=i;j++){
            dp[i+1][j]+=dp[i][j]*j;//i次求导后第j项为x的j次,再求导,得到下一次的系数
            dp[i+1][j]%=mod;
            dp[i+1][j+1]+=dp[i][j]*(n-j);//i次求导后(1+x)的次数为n-j,进行求导后加入x的j+1次中
            dp[i+1][j+1]%=mod;
        }
    }
    for(int i=1;i<=k;i++){
        ans=(ans+quick(2,n-i)*dp[k][i]%mod)%mod;//计算k次求导后,每一项的值,此处x代入1
    }
    printf("%lld\n",ans%mod);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值