Codeforces 932.E Team Work
2 seconds
256 megabytes
standard input
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.
Only line of input contains two integers N (1 ≤ N ≤ 109) representing total number of people and k (1 ≤ k ≤ 5000).
Output the sum of costs for all non empty subsets modulo 109 + 7.
1 1
1
3 2
24
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.
题目大意:求
.
分析:挺难的一道题,完全下不了手......
官方题解给出的做法非常巧妙.首先这个式子一眼看上去肯定不能直接解.变换一下?不会. 可以想到一个比较相似的式子:
.
那么怎么把这个式子变成题目给的式子呢?找一找不同,要求的式子r是从1开始的,这个式子r是从0开始的; 要求的式子r作为底数,k作为幂次,而这个式子凭空出现了一个x.变换比较难想到:
可以把这个式子求个导,然后乘x. 
求导可以将x的幂次中的r提出来一个,同时r=0时求导就变成了0.那么操作k次就能得到和要求的式子差不多的一个式子.有什么差别呢?式子中有x.
消除掉x的影响就可以了,想办法令x的幂次为1.可以利用dp.设f[a][b][c]表示还剩a次操作,式子
的值.如果b=0,c = n,同时令x = 1,那么这个式子操作k次后正好就是要求的式子.对这个式子操作一次,变成了:
,那么可以得到递推式:f[a][b][c] = b * f[a - 1][b][c] + c * f[a - 1][b + 1][c - 1].,最后答案是f[k][0][n].
看似是三次复杂度的dp,实际上是平方级别的,因为b和c的和一定等于n.那么dp只需要记录a和b的状态,c可以推出来. (不能记录c,因为c可能高达n,而实际上b和c最多只会变化k,b是从0开始的,所以记录b).
代码中的a,b表示上述分析中的b,c,一定要考虑边界情况:b = 0或c = 0,当a = 0时,就是一些连续的组合数相加,因为
中的x = 1,直接返回2^c.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; const ll mod = 1e9+; ll n,k,f[][]; ll qpow(ll a,ll b)
{
ll res = ;
while (b)
{
if (b & )
res = (res * a) % mod;
a = (a * a) % mod;
b >>= ;
}
return res % mod;
} ll dfs(ll dep,ll a)
{
if (f[dep][a] >= )
return f[dep][a];
ll b = n - a;
if (dep == )
return f[dep][a] = qpow(,b);
return f[dep][a] = ((b?1LL*b*dfs(dep-,a+):0LL)+(a?1LL*a*dfs(dep-,a):0LL))%mod;
} int main()
{
memset(f,-,sizeof(f));
cin >> n >> k;
cout << dfs(k,) % mod << endl; return ;
}
Codeforces 932.E Team Work的更多相关文章
- Codeforces 932 E. Team Work(组合数学)
http://codeforces.com/contest/932/problem/E 题意: 可以看做 有n种小球,每种小球有无限个,先从中选出x种,再在这x种小球中任选k个小球的方案数 选出的 ...
- Codeforces 932 E Team Work ( 第二类斯特林数、下降阶乘幂、组合数学 )
题目链接 题意 : 其实就是要求 分析 : 先暴力将次方通过第二类斯特林数转化成下降幂 ( 套路?) 然后再一步步化简.使得最外层和 N 有关的 ∑ 划掉 这里有个技巧就是 将组合数的表达式放到一边. ...
- 【CodeForces】932 E. Team Work
[题目]E. Team Work [题意]给定n和k,n个人中选择一个大小为x非空子集的代价是x^k,求所有非空子集的代价和%1e9+7.n<=10^9,k<=5000. [算法]斯特林反 ...
- Codeforces 757 F Team Rocket Rises Again
Discription It's the turn of the year, so Bash wants to send presents to his friends. There are n ci ...
- codeforces 108D Basketball Team(简单组合)
D. Basketball Team time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 122C perfect team
You may have already known that a standard ICPC team consists of exactly three members. The perfect ...
- Codeforces 107B Basketball Team 简单概率
题目链接:点击打开链接 题意: 给定n m h 表示有m个部门,有个人如今在部门h 以下m个数字表示每一个部门的人数.(包含他自己) 在这些人中随机挑选n个人,问挑出的人中存在和这个人同部门的概率是多 ...
- Codeforces 1016G Appropriate Team 数论 FWT
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1016G.html 题目传送门 - CF1016G 题意 给定 $n,x,y$ ,以及一个含有 $n$ 个元 ...
- Codeforces 932.F Escape Through Leaf
F. Escape Through Leaf time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- Ubuntu常用shell命令
目录 ls cd mkdir mv cp scp rm df du chmod chown chgrp head tail screen apt-get Ubuntu常用shell命令 Ubuntu作 ...
- 使用calendar日历插件实现动态展示会议信息
效果图如下,标红色为有会议安排,并跳转详细会议信息页面. html页面 <%@ page contentType="text/html;charset=UTF-8"%> ...
- Linux内核学习笔记(3)-- 进程的创建和终结
一. 进程创建: Unix 下的进程创建很特别,与许多其他操作系统不同,它分两步操作来创建和执行进程: fork() 和 exec() .首先,fork() 通过拷贝当前进程创建一个子进程:然后,ex ...
- 算法笔记(c++)--求一个数的所有质数因子
算法笔记(c++)--求一个数的所有质数因子 先贴题目: 这题不难,恶心在理解上面.最后看评论知道了怎么回事: 2*2*3*3*5=180 按照这逻辑的话应该输入的数由一系列质数相乘出来,所以每次找到 ...
- 城联数据TSM技术方案起底
近日,城联数据有限公司与中国电信签订了<基于NFC技术的公交业务的合作协议>.双方基于NFC技术开展互联互通城市公交卡业务合作,实现符合住房和城乡建设部城市公用事业互联互通卡系列标准的移动 ...
- AOP:Spring的xml配置方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 博弈---威佐夫博奕(Wythoff Game)
这个写的不错 威佐夫博奕(Wythoff Game):有两堆各若干个物品,两个人轮流从某一堆或同 时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜. 这种情况下是颇为复杂 ...
- HDU 1257 最少拦截系统(最长递减子序列的条数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 题解: #include<iostream> #include<cstdio ...
- 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]
题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...
- Alpha阶段敏捷冲刺 ADY8
一.举行站立式例会 今天也没有拍照片,人不齐. 二.团队报告 1.昨日已完成的工作 (1)创建一个test,并且将图片导入进去使其可以显示. 2.今日计划完成的工作 完成收尾工作.实现代码的连接. 3 ...