AtcoderGrandContest 005 F. Many Easy Problems
$ >AtcoderGrandContest \space 005 F. \ Many\ Easy\ Problems<$
题目大意 :
有一棵大小为 \(n\) 的树,对于每一个 \(k \in[1,n]\) ,求出在所有在树中选 \(k\) 个点的方案对应的包含这 \(k\) 个点的最小联通块大小之和\(1 \leq n \leq 2 \times 10^5\)
解题思路 :
容易发现,对于一组选取方案,包含它的最小联通块是唯一的,不妨考虑每一个点对这个联通块的贡献.
观察发现,一个点如果在一个最小联通块中,当且仅当有两个选取点的简单路径经过它
那么点 \(u\) 对 \(k\) 个点的贡献就是 \(C_n^k -\sum_{v} C_{sz[v]}^k-C_{n-sz[u]}^k\)
观察发现这个式子只和 \(sz\) 有关,不妨设 \(tot[i]\) 表示 \(sz[u]=i\) 的点的数量
考虑除根以外的每一个点只会在其父亲计算的时候被减去一个 \(C_{sz[u]}^k\) ,同时每一种 \(sz[u]\) 都会在计算大小为 \(n-sz[u]\) 的子树的时候被减去一次
所以 \(C_{sz[u]}^k\) 的被计算次数是 \(tot[sz[u]] + tot[n-sz[u]]\)
那么最终答案的式子就是 \(Ans_j =n \times C_n^j -\sum_{i=1}^n (tot[i]+tot[n-i])\times C_i^j\)
设 \(inv[i]\) 表示 \(i!\) 关于 \(Mod\) 的逆元,将后面的组合数拆开来可以得到
\(Ans_j =n \times C_n^j -\sum_{i=1}^n (tot[i]+tot[n-i])\times i! \times inv[j] \times inv[i-j]\)
设 \(A[i] = (tot[i]+tot[n-i])\times i!\) ,则 \(Ans_j = n \times C_n^j \times inv[j] - \sum_{i=1}^nA[i]\times inv[i-j]\), 后者 \(NTT\) 进行计算即可
/*program by mangoyang*/
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
const int N = 1605005, L = 200005, P = 924844033, G = 5;
vector<int> g[N];
int inv[N], iv[N], s[N], f[N], tot[N], sz[N], js[N], n;
inline int Pow(int a, int b){
int ans = 1;
for(; b; b >>= 1, a = a * a % P)
if(b & 1) ans = ans * a % P;
return ans;
}
namespace NTT{
int rev[N];
inline int Getrev(int ned){
int lg = 0, len = 1;
for(; len <= ned; len <<= 1, lg++);
for(int i = 0; i < len; i++)
rev[i] = (rev[i>>1] >> 1) | ((i & 1) << (lg - 1));
return len;
}
inline void DFT(int *A, int len, int type){
for(int i = 0; i < len; i++) if(i < rev[i]) swap(A[i], A[rev[i]]);
for(int k = 2; k <= len; k <<= 1){
int w = Pow(G, (P - 1) / k); if(type == -1) w = Pow(w, P - 2);
for(int i = 0; i < len; i += k){
int now = 1;
for(int j = i; j < i + (k >> 1); j++, (now *= w) %= P){
int x = A[j], y = (now * A[j+(k>>1)]) % P;
A[j] = (x + y) % P, A[j+(k>>1)] = (x - y + P) % P;
}
}
}
if(type == -1){
int now = Pow(len, P - 2);
for(int i = 0; i < len; i++) (A[i] *= now) %= P;
}
}
inline void Times(int *A, int *B, int lena, int lenb){
int len = Getrev(lena + lenb + 1);
DFT(A, len, 1), DFT(B, len, 1);
for(int i = 0; i < len; i++) A[i] = A[i] * B[i] % P;
DFT(A, len, -1);
}
}
inline void dfs(int u, int fa){
sz[u] = 1, f[u] = fa;
for(int i = 0; i < g[u].size(); i++)
if(g[u][i] != fa) dfs(g[u][i], u), sz[u] += sz[g[u][i]];
}
inline int C(int x, int y){
return js[x] * inv[y] % P * inv[x-y] % P;
}
signed main(){
read(n), js[0] = 1, inv[0] = iv[L] = 1;
for(int i = 1; i <= n; i++){
js[i] = js[i-1] * i % P;
iv[L-i] = inv[i] = Pow(js[i], P - 2);
}
for(int i = 1, x, y; i < n; i++){
read(x), read(y);
g[x].push_back(y), g[y].push_back(x);
}
dfs(1, 0);
for(int i = 2; i <= n; i++) tot[sz[i]]++;
for(int i = 1; i <= n; i++)
s[L+i] = (tot[i] + tot[n-i]) * js[i] % P;
NTT::Times(s, iv, L + n + 1, L + n + 1);
for(int i = 1; i <= n; i++){
int A = n * C(n, i) % P;
int B = inv[i] * s[2*L+i] % P;
printf("%lld\n", ((A - B) % P + P) % P);
}
return 0;
}
AtcoderGrandContest 005 F. Many Easy Problems的更多相关文章
- [题解] Atcoder AGC 005 F Many Easy Problems NTT,组合数学
题目 观察当k固定时答案是什么.先假设每个节点对答案的贡献都是\(\binom{n}{k}\),然后再减掉某个点没有贡献的选点方案数.对于一个节点i,它没有贡献的方案数显然就是所有k个节点都选在i连出 ...
- 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT
[题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...
- 解题:AT2064 Many Easy Problems&EXNR #1 T3 两开花
题面 两道题比较像,放在一起写了,后者可以看成前者的加强版 (sto ztb orz) 先看AT那道题 考虑计算每个点的贡献,用容斥计算:每个点没有贡献当且仅当选的所有点都在以他为根时的一个子节点的子 ...
- Codeforces 913D - Too Easy Problems
913D - Too Easy Problems 思路:二分check k 代码: #include<bits/stdc++.h> using namespace std; #define ...
- 【CodeForces】913 D. Too Easy Problems
[题目]D. Too Easy Problems [题意]给定n个问题和总时限T,每个问题给定时间ti和限制ai,当解决的问题数k<=ai时问题有效,求在时限T内选择一些问题解决的最大有效问题数 ...
- 【AGC005 F】Many Easy Problems
神他吗一天考一道码农题两道 FFT(其实还是我推式子一窍不通) 题意 给你一棵 \(n\) 个点的树,再给你一个常数 \(k\). 设 \(S\) 为树上某些点的集合,定义 \(f(S)\) 为最小的 ...
- 【AGC 005F】Many Easy Problems
Description One day, Takahashi was given the following problem from Aoki: You are given a tree with ...
- AtCoder - 2064 Many Easy Problems
Problem Statement One day, Takahashi was given the following problem from Aoki: You are given a tree ...
- Codeforces B. Too Easy Problems
题目描述: time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
随机推荐
- angular package.json中start build
"start": "ng serve --host 0.0.0.0 --port 4200 --proxy-config proxy.conf.json", & ...
- easyUI导出数据
easyUI导出数据模式 后台: //导出数据 public function index_doExport() { $search['diqu']=$_POST['diqu']; $search[' ...
- JSP和Servlet面试题
1.讲下servlet的执行流程. Servlet的执行流程也就是servlet的生命周期,当服务器启动的时候生命周期开始,然后通过init()<启动顺序根据web.xml里的startup-o ...
- E.Text Editor (Gym 101466E + 二分 + kmp)
题目链接:http://codeforces.com/gym/101466/problem/E 题目: 题意: 给你s串和t串,一个数k,求t的最长前缀串在s串中出现次数不少于k. 思路: 一眼二分+ ...
- 数组中的each 和 jquery 中的 each
数组的实例上都有一个叫做 forEach 的方法,这个方法定义在 Array.prototype 上,所以数组的所有实例都可以使用 forEach 这个方法. forEach 方法的语法结构如下: v ...
- 去除IE10+上文本框巨丑无比的删除图标以及显示密码图标
去除IE10+上文本框巨丑无比的删除图标以及显示密码图标 IE浏览器总是让人喜欢让人厌,在最新的IE浏览器(IE10+)上使用表单时,文本框内后面会出现很巨丑无比的“删除图标”以及“显示密码图标”,如 ...
- ubuntu永久修改主机名
1.查看主机名 在Ubuntu系统中,快速查看主机名有多种方法:其一,打开一个GNOME终端窗口,在命令提示符中可以看到主机名,主机名通常位于“@”符号后:其二,在终端窗口中输入命令:hostname ...
- 初识PDO数据库抽象层
目录: 00x1 php中的pdo是什么? 00x2 pdo创建一个PDO对象 00x1 php中的pdo是什么? 就是操作数据库的方法,pdo就是把操作数据库的函数封装成一个pdo类,其间做了安全验 ...
- mysql安装后开启远程
操作系统为centos7 64 1.修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证 2. ...
- python设计模式之装饰器详解(三)
python的装饰器使用是python语言一个非常重要的部分,装饰器是程序设计模式中装饰模式的具体化,python提供了特殊的语法糖可以非常方便的实现装饰模式. 系列文章 python设计模式之单例模 ...