codeforces 955F Cowmpany Cowmpensation 树上DP+多项式插值
给一个树,每个点的权值为正整数,且不能超过自己的父节点,根节点的最高权值不超过D
问一共有多少种分配工资的方式?
题解:
A immediate simple observation is that we can compute the answer in $O(nD) $with a simple dynamic program. How to speed it up though?
To speed it up, we need the following lemma.
Lemma 1: For a tree with nn vertices, the answer is a polynomial in $D$ of degree at most nn.
We can prove this via induction, and the fact that for any polynomial $ p(x) $ of degree dd, the sum $\sum p_i $ is a polynomial in $n$ of degree $d+1$
Now the solution is easy: compute the answerfor $ 0≤D≤n $ and use interpolation to compute the answer for $ D>n $.
The complexity is $O(n^2)$ or the initial dp and $O(n)$ for the interpolation step.
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mp make_pair
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#pragma GCC optimize("unroll-loops")
#define inline inline __attribute__( \
(always_inline, __gnu_inline__, __artificial__)) \
__attribute__((optimize("Ofast"))) __attribute__((target("sse"))) \
__attribute__((target("sse2"))) __attribute__((target("mmx")))
#define IO ios::sync_with_stdio(false);
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define for_node(x,i) for(int i=head[x];i;i=e[i].next)
#define show(x) cout<<#x<<"="<<x<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="baidu<a[b]<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
using namespace std;
const int maxn=4e3+10,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
const double PI=acos(-1.0);
//head
ll casn,n,m,k;
ll num[maxn];
ll pow_mod(ll a,ll b,ll c=mod,ll ans=1){while(b){if(b&1) ans=(a*ans)%c;a=(a*a)%c,b>>=1;}return ans;}
namespace polysum {
const int maxn=101000;
const ll mod=1e9+7;
ll a[maxn],f[maxn],g[maxn],p[maxn],p1[maxn],p2[maxn],b[maxn],h[maxn][2],C[maxn];
ll calcn(int d,ll *a,ll n) {//d´Î¶àÏîʽ(a[0-d])ÇóµÚnÏî
if (n<=d) return a[n];
p1[0]=p2[0]=1;
rep(i,0,d) {
ll t=(n-i+mod)%mod;
p1[i+1]=p1[i]*t%mod;
}
rep(i,0,d) {
ll t=(n-d+i+mod)%mod;
p2[i+1]=p2[i]*t%mod;
}
ll ans=0;
rep(i,0,d) {
ll t=g[i]*g[d-i]%mod*p1[i]%mod*p2[d-i]%mod*a[i]%mod;
if ((d-i)&1) ans=(ans-t+mod)%mod;
else ans=(ans+t)%mod;
}
return ans;
}
void init(int maxm) {//³õʼ»¯Ô¤´¦Àí½×³ËºÍÄæÔª(ȡģ³Ë·¨)
f[0]=f[1]=g[0]=g[1]=1;
rep(i,2,maxm+4) f[i]=f[i-1]*i%mod;
g[maxm+4]=pow_mod(f[maxm+4],mod-2);
per(i,1,maxm+3) g[i]=g[i+1]*(i+1)%mod;
}
}
ll dp[maxn][maxn];
struct node {int to,next;}e[maxm];int head[maxn],nume;
void add(int a,int b){e[++nume]=(node){b,head[a]};head[a]=nume;} void dfs(int now){
rep(i,1,n) dp[now][i]=1;
for(int i=head[now];i;i=e[i].next){
dfs(e[i].to);
rep(j,1,n) dp[now][j]=dp[now][j]*dp[e[i].to][j]%mod;
}
rep(i,1,n) dp[now][i]=(dp[now][i]+dp[now][i-1])%mod;
} int main() {
//#define test
#ifdef test
auto _start = chrono::high_resolution_clock::now();
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif IO;
cin>>n>>k;
rep(i,2,n){
int a;
cin>>a;
add(a,i);
}
dfs(1);
polysum::init(n+1);
rep(i,0,n+1){
num[i]=dp[1][i];
}
ll ans=polysum::calcn(n,num,k);
cout<<ans<<endl; #ifdef test
auto _end = chrono::high_resolution_clock::now();
cerr << "elapsed time: " << chrono::duration<double, milli>(_end - _start).count() << " ms\n";
fclose(stdin);fclose(stdout);system("out.txt");
#endif
return 0;
}
codeforces 955F Cowmpany Cowmpensation 树上DP+多项式插值的更多相关文章
- [CF995F]Cowmpany Cowmpensation[树形dp+拉格朗日插值]
题意 给你一棵树,你要用不超过 \(D\) 的权值给每个节点赋值,保证一个点的权值不小于其子节点,问有多少种合法的方案. \(n\leq 3000, D\leq 10^9\) 分析 如果 \(D\) ...
- Codeforces 995F Cowmpany Cowmpensation - 组合数学
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个棵$n$个点的有根树和整数$D$,给这$n$个点标号,要求每个节点的标号是正整数,且不超过父节点的标号,根节点的标号不得超过D. 很容 ...
- 整数拆分 [dp+多项式插值]
题意 $1 \leq n \leq 10^{18}$ $2 \leq m \leq 10^{18}$ $1 \leq k \leq 20$ 思路 n,m较小 首先考虑朴素的$k=1$问题: $f[i] ...
- Codeforces F. Cowmpany Cowmpensation
Description 有一棵树,现在要给每个节点赋一个在1到D之间的权值,问有多少种方案满足任意一个节点的权值都不大于其父亲的权值. n<=3000,D<=1e9 题面 Solution ...
- 【CF995F】Cowmpany Cowmpensation(动态规划,拉格朗日插值)
[CF995F]Cowmpany Cowmpensation(多项式插值) 题面 洛谷 CF 题解 我们假装结果是一个关于\(D\)的\(n\)次多项式, 那么,先\(dp\)暴力求解颜色数为\(0. ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- CodeForces 690C2 Brain Network (medium)(树上DP)
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- 【CF995F】 Cowmpany Cowmpensation
CF995F Cowmpany Cowmpensation Solution 这道题目可以看出我的代码能力是有多渣(代码能力严重退化) 我们先考虑dp,很容易写出方程: 设\(f_{i,j}\)表示以 ...
随机推荐
- 12.代理模式(Proxy Pattern)
直接与间接: 人们对复杂的软件系统常有一种处理手法,即增加一层间接层,从而对系统获得一种更为灵活.满足特定需求的解决方案. ...
- Nginx记录-nginx 负载均衡5种配置方式(转载)
nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成 ...
- HTML汇总以及CSS的一些开端
一.HTML初探 1.HTML(HyperText Markup Language):超文本标记语言指的就是超跃了txt文件,能够在里面进行一些例如:就是指页面内可以包含图片.链接 .甚至音乐.程序等 ...
- expdp和impdp导入导出用法【转】
关于expdp和impdp exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.expdp和impdp是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用.i ...
- css 动画【转】
css 动画 http://www.w3school.com.cn/css3/css3_animation.asp
- js中文编码到C#后台解码
escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编 ...
- 2016-2017-2 20155324实验二《Java面向对象程序设计》实验报告
2016-2017-2 20155324实验二<Java面向对象程序设计>实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉 ...
- 关于ajax及相关数据传输问题
之前整理的ajax相关应用笔记,一直没有时间整理,今天突然翻到特此将初稿大概的整理了一下,可能有点乱,欢迎指出不足之处. jQuery的ajax请求:complete函数一般无论服务器有无数据返回都会 ...
- jq的error
error事件会在js发生错误或资源加载失败时触发.该事件主要用于window对象.<img>等元素. 此外,你可以为同一元素多次调用该函数,从而绑定多个事件处理函数.触发error事件时 ...
- Python 基础算法
递归 时间&空间复杂度 常见列表查找 算法排序 数据结构 递归 在调用一个函数的过程中,直接或间接地调用了函数本身这就叫做递归. 注:python在递归中没用像别的语言对递归进行优化,所以每一 ...