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. Android消息推送怎么实现?

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  2. Delphi中record和packed record的区别

    转载:http://blog.csdn.net/rznice/article/details/6566978 第一种不带packed关键字的结构体表明编译器编译时要求进行字对齐. 而第二种带packe ...

  3. 【翻译十三】java-并发之饥饿与活锁

    Starvation and Livelock Starvation and livelock are much less common a problem than deadlock, but ar ...

  4. 《linux系统及其编程》实验课记录(六)

    实验 6:Linux 文件系统 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统.有另外一个无特权用户 student,密码 student ...

  5. java 杂物间 (一) Mybatis

    这里放置的是一些杂物,生人勿入. Token的一般parse 过程. @Test public void shouldDemonstrateGenericTokenReplacement() { Ge ...

  6. 腾讯微博的账号登录及api操作

    .tqq.php <?php /** * PHP Library for t.qq.com * * @author */ class tqqPHP { function __construct( ...

  7. myeclipse报错: java compiler level does not match the version of the installed java project facet

    在升级到myeclipse 9.0正式版后,很无耐地出发现了一个error级别的错误,虽然没在代码中,但是看着让人很不舒服.第一反应就是到网上搜索解决之道,结果,网站说在工程的属性中去找个叫啥&quo ...

  8. java Integer和int的拆箱与装箱

    官网:http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 1.赋值: a. 把int类型赋值给Integer类型:JVM会 ...

  9. WPF线程(Step1)——Dispatcher

    使用WPF开发时经常会遇上自己建立的线程需要更新界面UI内容,从而导致的跨线程问题. 异常内容: 异常类型:System.InvalidOperationException 异常描述: "S ...

  10. NDK-gdb

    http://www.gnu.org/software/gdb/download/ http://mhandroid.wordpress.com/2011/01/23/using-eclipse-fo ...