Description

给定一棵\(n\le 10^5\)的树, 和\(k\le 150\)

求每个点\(x\)的$$S(x) = \sum_{y=1}^n dis(x, y) ^ k$$

Analysis

k比较小, 考虑斯特林展开

\[\begin{aligned}
S(x) &= \sum_{y=1}^n dis(x, y)^k\\
&=\sum_{y=1}^n \sum_{i=0}^k \left\{\begin{matrix}k\\i\end{matrix}\right\} i!\binom{dis(x,y)}{i}\\
&=\sum_{i=0}^k \left\{\begin{matrix}k\\i\end{matrix}\right\} i! \sum_{y=1}^n \binom{dis(x,y)}{i}\\
\end{aligned}
\]

考虑求后面的部分, 可以上下树形dp一下

记$$f[x][i] = \sum_{y\in sub(x)} \binom{dis(x, y)}{i}$$

那么$$f[x][i] = \sum_{y\in son(x)} f[y][i] + [i>0]f[y][i-1]$$

即利用$$\Delta \binom x c = \binom x {c-1}$$

这题只用暴力转移组合数一次

Code

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#define rep(i,a,b) for (int i = (a); i <= (b); ++ i)
#define per(i,a,b) for (int i = (a); i >= (b); -- i)
#define For(i,a,b) for (int i = (a); i < (b); ++ i)
using namespace std;
const int N = 5e4 + 7;
const int M = 157;
const int Q = 10007; inline int ri(){
int x = 0; bool f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
for (; isdigit(c); c = getchar()) x = x*10+c-48;
return f ? x : -x;
} inline int pls(int x, int y) {return (x + y) % Q;}
inline int mns(int x, int y) {return pls(x, Q - y);}
inline int mul(int x, int y) {return x * y % Q;} int n, m;
int fac[M], S2[M][M];
int f[N][M], g[N][M];
int ans[N]; struct vec {
int g[N], te;
struct edge{int y, nxt;}e[N << 1];
inline void push(int x, int y) {e[++te] = (edge){y, g[x]}; g[x] = te;}
inline void push2(int x, int y) {push(x, y); push(y, x);}
inline int& operator () (int x) {return g[x];}
inline edge& operator [] (int x) {return e[x];}
}e; void upd(int *A, int *B, int kd = 1) {
if (kd == 1) {
rep (i, 0, m) A[i] = pls(A[i], B[i]);
rep (i, 1, m) A[i] = pls(A[i], B[i-1]);
}
if (kd == -1) {
rep (i, 0, m) A[i] = mns(A[i], B[i]);
rep (i, 1, m) A[i] = mns(A[i], B[i-1]);
}
} void UP(int x, int fa) {
int p, y; rep (i, 0, m) ans[x] = pls(ans[x], mul(S2[m][i], pls(g[x][i], f[x][i]))); for (p=e(x); p; p=e[p].nxt)
if ((y=e[p].y) != fa) {
upd(f[x], f[y], -1); upd(g[y], f[x]);
upd(g[y], g[x]);
UP(y, x); upd(f[x], f[y]);
}
} void DW(int x, int fa) {
int p, y;
for (p=e(x); p; p=e[p].nxt)
if ((y=e[p].y) != fa) {
DW(y, x);
upd(f[x], f[y]);
}
f[x][0] = pls(f[x][0], 1); // d(x, x) = 0
} void Uncompress() {
int L, i, now, A, B, mod, tmp;
scanf("%d%d%d", &n, &m, &L);
scanf("%d%d%d%d", &now, &A, &B, &mod);
for (i = 1; i < n; i ++) {
now = (now * A + B) % mod;
tmp = (i < L) ? i : L;
e.push2(i - now % tmp, i + 1);
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
#endif Uncompress(); int i, j;
for (fac[0] = 1, i=1; i<=m; ++i) fac[i] = mul(fac[i-1], i); for (S2[0][0] = 1, i=1; i<=m; ++i)
for (j=1; j<=i; ++j) S2[i][j] = pls(mul(j, S2[i-1][j]), S2[i-1][j-1]);
rep (i, 0, m) rep (j, 0, i) S2[i][j] = mul(S2[i][j], fac[j]); DW(1, 0);
UP(1, 0); rep (i, 1, n) printf("%d\n", ans[i]); return 0;
}

bzoj 2159 - Crash 的 文明世界的更多相关文章

  1. bzoj 2159: Crash 的文明世界

    Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 480  Solved: 234[Submit][Status][Discuss] Descripti ...

  2. BZOJ.2159.Crash的文明世界(斯特林数 树形DP)

    BZOJ 洛谷 挺套路但并不难的一道题 \(Description\) 给定一棵\(n\)个点的树和\(K\),边权为\(1\).对于每个点\(x\),求\(S(x)=\sum_{i=1}^ndis( ...

  3. BZOJ 2159: Crash 的文明世界(树形dp+第二类斯特林数+组合数)

    题意 给定一棵 \(n\) 个点的树和一个常数 \(k\) , 对于每个 \(i\) , 求 \[\displaystyle S(i) = \sum _{j=1} ^ {n} \mathrm{dist ...

  4. bzoj 2159 Crash 的文明世界 && hdu 4625 JZPTREE ——第二类斯特林数+树形DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2159 学习材料:https://blog.csdn.net/litble/article/d ...

  5. bzoj 2159 Crash 的文明世界 & hdu 4625 JZPTREE —— 第二类斯特林数+树形DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2159 使用公式:\( n^{k} = \sum\limits_{i=0}^{k} S(k,i ...

  6. [bzoj 2159]Crash的文明世界

    今天看到一个鬼题 心情好的时候写 [题意]求树上所有点对距离的k次方和,所有边权为1 大爷方的题解:http://tonyfang.is-programmer.com/posts/204972.htm ...

  7. BZOJ 2159: Crash 的文明世界 第二类斯特林数+树形dp

    这个题非常巧妙啊~ #include <bits/stdc++.h> #define M 170 #define N 50003 #define mod 10007 #define LL ...

  8. BZOJ 2159: Crash 的文明世界(组合数学+第二类斯特林数+树形dp)

    传送门 解题思路 比较有意思的一道数学题.首先\(n*k^2\)的做法比较好想,就是维护一个\(x^i\)这种东西,然后转移的时候用二项式定理拆开转移.然后有一个比较有意思的结论就是把求\(x^i\) ...

  9. 【BZOJ2159】Crash的文明世界(第二类斯特林数,动态规划)

    [BZOJ2159]Crash的文明世界(第二类斯特林数,动态规划) 题面 BZOJ 洛谷 题解 看到\(k\)次方的式子就可以往二项式的展开上面考,但是显然这样子的复杂度会有一个\(O(k^2)\) ...

随机推荐

  1. Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)

    出现  Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)时在php.ini文件中配置 memor ...

  2. mysql的一些相关的命令(2013-05-05-bd 写的日志迁移

    cmd中连接:mysql -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit (回车) --建立数据库creata database 数据库名;--切换到数据库下工 ...

  3. QToolBox学习笔记

    抽屉控件效果类似于QQ界面 最外面一层叫工具盒QToolBox QToolBox中装的是QGroupBox,分组的盒子 在分组的盒子QGroupBox中装的是QToolButton.

  4. openwrt(一):openwrt源码下载及编译环境搭建

    声明:从网上各位大神的博客学习,整理后记录,非原创. 注:请用非root用户来下载源码 导航: 1. openwrt编译环境搭建 2. openwrt源码下载 3. feeds更新 1. openwr ...

  5. linux处理僵尸进程

    由来 在linux下,如果一个进程终止,内核会释放该进程使用的所有存储区,关闭所有文件句柄等,但是,内核会为每个终止子进程保留一定量的信息.这些信息至少包括进程ID,进程的终止状态,以及该进程使用的C ...

  6. C++基础 inline 默认参数 函数占位参数 函数重载

    1. inline内联函数 内联函数用于替换宏, 实例: 其中宏和 ++ 连用有副作用. #include "iostream" using namespace std; #def ...

  7. C# 创建新线程

    首先需要包含命名空间 using System.Threading; 然后创建进程 Thread th = new Thread(new ThreadStart(ThreadMethod)); //创 ...

  8. powershell设置SS代理

    $env:HTTPS_PROXY="http://127.0.0.1:1080" $env:HTTP_PROXY="http://127.0.0.1:1080"

  9. idea无法新建maven项目

    之前用的都是eclipse,自从4月底入职新公司后,接触到了idea. 然后自己的电脑上也安装了idea,不过一直都没用,直到昨天打算开起来使用一下. 之后就是想新建一个maven项目,发现死活也新建 ...

  10. mutable c++

    The keyword mutable is used to allow a particular data member of const object to be modified. This i ...