CF932E Team Work Number Theory

topic

https://codeforces.com/contest/932/problem/E

Chinese title: There are N (1≤N≤1e9) different people, from which you choose x (x is at least 1) individuals to complete a team task at a cost of xk (1≤k≤5000). Find the total cost of all team plans.

About the four arithmetic operations of derivatives: https://www.cnblogs.com/liming19680104/p/10955536.html

answer

It is easy to draw the question requirements① \sum_{i=0}^{n}C_{n}^{i}i^{k}, where i=0 does not affect the answer.

C_{n}^{i}It is easy to think of the binomial theorem when you see : ② (x+1)^{n}=\sum_{i=0}^{n}C_{n}^{i}x^{i},

① is very similar to ②, and ② is much better than ①. How to change ② into ①?

The answer is: seek derivation

② If the derivative of formula is multiplied by x, then x is multiplied by the derivative, repeat, becomes \sum_{i=0}^{n}C_{n}^{i}ix^{i}, \sum_{i=0}^{n}C_{n}^{i}i^{2}x^{i}, \sum_{i=0}^{n}C_{n}^{i}i^{3}x^{i}..., k times becomes later reuse \sum_{i=0}^{n}C_{n}^{i}i^{k}x^{i}, since x is a number we arbitrarily taken, to make it equal to 1. The right side of formula ② finally becomes \sum_{i=0}^{n}C_{n}^{i}i^{k}, so just calculate the derivative result of the left side of formula ② to get the answer

②The transformation process on the left side of the formula:

once:(x+1)^{n}\rightarrow n(x+1)^{n-1}x

At this time the function becomes two functions  n(x+1)^{n-1}, x multiplication, according to the derivative of the four arithmetic operationsd[f(x)g(x)]=f(x)d[g(x)]+g(x)d[f(x)]

The following transformations can be made:n(x+1)^{n-1}x\rightarrow n(x+1)^{n-1}x+n(n-1)(x+1)^{n-2}x^{2}

In this way  , each term of the form  a_{j}(x+1)^{n-j}x^{j}a_{j}for the coefficient of this term) can be changed to  ja_ {j} (x + 1) ^ {nj} x ^ {j} + (nj) a_ {j} (x + 1) ^ {n- (j + 1)} x ^ {j + 1},

Perform k times of transformation, and produce k items at most, and j>nthe elimination of items is not counted. Use dp to maintain the coefficient and complexityO(k^{2})

Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define MAXN 5005
#define MOD 1000000007ll
using namespace std;
inline ll read(){
	ll x=0,f=1;char s=getchar();
	while((s<'0'||s>'9')&&s>0){if(s=='-')f*=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=(x<<1)+(x<<3)+s-'0';s=getchar();}
	return x*f;
}
int k;
ll n,dp[MAXN],ans;
inline ll ksm(ll a,ll b,ll mo){
	ll res=1;
	for(;b;b>>=1){
		if(b&1)res=res*a%mo;
		a=a*a%mo;
	}
	return res;
}
int main()
{
	n=read(),k=read();
	dp[1]=n;
	for(int i=2;i<=k;i++)
		for(int j=i;j>0;j--)
			dp[j]=(dp[j-1]*(n-j+1)+dp[j]*j)%MOD;
	for(int i=1;i<=k;i++)
		if(n-i>=0)ans=(ans+dp[i]*ksm(2,n-i,MOD))%MOD;//令x等于1
	printf("%lld\n",ans);
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43960287/article/details/108931944