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. Python 自然语言处理(1) 计数词汇

    Python有一个自然语言处理的工具包,叫做NLTK(Natural Language ToolKit),可以帮助你实现自然语言挖掘,语言建模等等工作.但是没有NLTK,也一样可以实现简单的词类统计. ...

  2. 【翻译十六】java-固定对象的定义方法

    A Strategy for Defining Immutable Objects The following rules define a simple strategy for creating ...

  3. [译]关于.NET Core1.1的通告

    以下翻译可能会有不准确的地方, 想看原文的童鞋移步到Announcing .NET Core 1.1, 微软的开源真心喜欢, 希望有更多的童鞋关注微软, 关注.NET Core 我们很兴奋地宣布.NE ...

  4. HDU5792 World is Exploding(树状数组)

    一共6种情况,a < b且Aa < Ab, c < d 且Ac > Ad,这两种情况数量相乘,再减去a = c, a = d, b = c, b = d这四种情况,使用树状数组 ...

  5. scala中的抽象类

    scala中也有和java,c#类似的抽象类,抽象类会有部分实现,也有没有实现的方法定义.抽象类最大的特征是不能直接实例化.下面我们看个例子. abstract class Animal { def ...

  6. Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树

    E. Sign on Fence   Bizon the Champion has recently finished painting his wood fence. The fence consi ...

  7. 【SSH】 之 Struts2

    (一)Struts2是什么? Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与 ...

  8. string、math、random、datetime类

    1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确            ...

  9. 使用Hue上传hive数据

    大概逻辑是先上传hdfs数据,然后创建hive外部表,关联到hdfs上传数据的位置. 截图比较概要,但是用起来很简单 1.创建路径和上传文件 2.创建外部表

  10. json 转化

    1. 把java 对象列表转换为json对象数组,并转为字符串 复制代码代码如下:     JSONArray array = JSONArray.fromObject(userlist);    S ...