http://codeforces.com/contest/438/problem/E

题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数。请给出每个$s \in [1,S]$ 对应的答案。($S,m < 10^5$)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=(1e5+10)*4, mo=998244353;
int two, G[30], nG[30], rev[N];
int ipow(int a, int b) { int x=1; for(; b; b>>=1, a=(ll)a*a%mo) if(b&1) x=(ll)x*a%mo; return x; }
void fft_init() {
two=ipow(2, mo-2); G[23]=ipow(3, (mo-1)/(1<<23)); nG[23]=ipow(G[23], mo-2);
for(int i=22; i; --i) G[i]=(ll)G[i+1]*G[i+1]%mo, nG[i]=(ll)nG[i+1]*nG[i+1]%mo;
}
int getlen(int n) {
int len=1, bl=-1;
for(; len<n; len<<=1, ++bl);
for(int i=1; i<len; ++i) rev[i]=(rev[i>>1]>>1)|((i&1)<<bl);
return len;
}
void fft(int *a, int n, int f) {
for(int i=0; i<n; ++i) if(i<rev[i]) swap(a[i], a[rev[i]]);
for(int m=2, now=1; m<=n; m<<=1, ++now) {
int mid=m>>1, w=1, wn=G[now], u, v; if(f) wn=nG[now];
for(int i=0; i<n; i+=m, w=1)
for(int j=0; j<mid; ++j) {
u=a[i+j], v=(ll)a[i+j+mid]*w%mo;
a[i+j]=(u+v)%mo; a[i+j+mid]=(u-v+mo)%mo; w=(ll)w*wn%mo;
}
}
}
void getinv(int *a, int *b, int n) {
if(n==1) { b[0]=ipow(a[0], mo-2); return; }
getinv(a, b, (n+1)>>1);
static int c[N], d[N];
memcpy(c, a, sizeof(int)*(n)); memcpy(d, b, sizeof(int)*((n+1)>>1));
int len=getlen(n+n-1), nlen=ipow(len, mo-2);
fft(c, len, 0); fft(d, len, 0);
for(int i=0; i<len; ++i) d[i]=(ll)d[i]*(2-(ll)d[i]*c[i]%mo+mo)%mo;
fft(d, len, 1);
for(int i=0; i<n; ++i) b[i]=(ll)d[i]*nlen%mo;
memset(c, 0, sizeof(int)*(len)); memset(d, 0, sizeof(int)*(len));
}
void getroot(int *a, int *b, int n) {
if(n==1) { b[0]=sqrt(a[0]); return; }
getroot(a, b, (n+1)>>1);
static int c[N], d[N];
memcpy(c, a, sizeof(int)*(n));
getinv(b, d, n);
int len=getlen(n+n-1), nlen=ipow(len, mo-2);
fft(c, len, 0); fft(d, len, 0);
for(int i=0; i<len; ++i) d[i]=(ll)c[i]*d[i]%mo;
fft(d, len, 1);
for(int i=0; i<n; ++i) b[i]=(ll)two*((b[i]+(ll)d[i]*nlen%mo)%mo)%mo;
memset(d, 0, sizeof(int)*(len)); memset(c, 0, sizeof(int)*(len));
}
int a[N], b[N];
int main() {
fft_init();
int m, n; scanf("%d%d", &n, &m);
for(int i=0; i<n; ++i) { int x; scanf("%d", &x); if(x<=m) a[x]=mo-4; }
a[0]=1;
getroot(a, b, m+1);
b[0]=(b[0]+1)%mo;
getinv(b, a, m+1);
for(int i=1; i<=m; ++i) printf("%d\n", (a[i]<<1)%mo);
return 0;
}

  

多项式求根= =具体看picks博客..http://picks.logdown.com/posts/202388-square-root-of-polynomial

其实想到了母函数然后知道用倍增来求根本题就解决了= =...跪跪跪orz

一定要注意那些多项式的次数啊!!!一定要想明白啊!!!

【CF】438E. The Child and Binary Tree的更多相关文章

  1. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  2. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  3. 【leetcode】979. Distribute Coins in Binary Tree

    题目如下: Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and th ...

  4. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. 【LeetCode】104 - Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. 【LeetCode】111 - Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

    求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  8. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

    求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  9. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

随机推荐

  1. Sphinx的介绍和原理探索

    What/Sphinx是什么 定义 Sphinx是一个全文检索引擎. 特性 索引和性能优异 易于集成SQL和XML数据源,并可使用SphinxAPI.SphinxQL或者SphinxSE搜索接口 易于 ...

  2. 七牛---以一个七牛上传的实例小结下AJAX跨域【转】

    http://blog.csdn.net/netdxy/article/details/50699842 使用七牛过程中,很多用户或多或少遇到跨域的问题,这篇文章主要介绍下跨域的概念来看什么情况下会出 ...

  3. JavaWeb学习之tomcat安装与运行、tomcat的目录结构、配置tomcat的管理用户、web项目目录、虚拟目录、虚拟主机(1)

    1.tomcat安装与运行双击tomcat目录下的bin/startup.bat,启动之后,输入http://localhost:8080,出现安装成功的提示,表示安装tomcat成功 2.tomca ...

  4. Win10开机提示Resume from Hibernation该怎么办?

    Windows10系统的电脑开机提示:Resume from Hibernation(从休眠恢复),这是电脑没有真正关机,而是上次关机时进入了[休眠状态],所以开机时提示:从休眠恢复.如何解决Wind ...

  5. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

  6. .Net Ioc Unity

    Unity 的接口IUnityContainer public interface IUnityContainer : IDisposable IUnityContainer RegisterType ...

  7. [Qcon] 百姓网开发总结

    拿到的PPT看了之后,发现给出的很简洁,但每个步骤用处却非常有用,我们一个个来分析: 1. 集中开发环境,这些方法看似简单,但是都是很实用的方法,在我开发中看的出来,SVN无分支就能解决我现有部门的部 ...

  8. ICP 算法步骤

    The Iterative Closest Point (ICP) is an algorithm employed to match two surface representations, suc ...

  9. Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA

    E. Cactus   A connected undirected graph is called a vertex cactus, if each vertex of this graph bel ...

  10. loj 1032 数位dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ...