链接:https://www.nowcoder.com/acm/contest/133/D
来源:牛客网

题目描述

Applese打开了m个QQ群,向群友们发出了组队的邀请。作为网红选手,Applese得到了n位选手的反馈,每位选手只会在一个群给Applese反馈
现在,Applese要挑选其中的k名选手组队比赛,为了维持和各个群的良好关系,每个群中都应有至少一名选手成为Applese的队友(数据保证每个群都有选手给Applese反馈)
Applese想知道,他有多少种挑选队友的方案

输入描述:

输入包括两行
第一行包括三个数n, m, k,表示共有n位选手,m个群,需要有k名选手被选择
第二行包括m个数,第i个数表示第i个群有si个选手
n ≤ 100000, m ≤ k ≤ n

输出描述:

输出包括一行
第一行输出方案数
由于输出可能比较大,你只需要输出在模998244353意义下的答案

输入例子:
5 3 4
1 2 2
输出例子:
4

-->

示例1

输入

5 3 4
1 2 2

输出

4

析:由于每个群都要选人,而且每个人还不同,从一个 n 个人的群里选 m 个人,方法数是 C(n, m)。但是要考虑多个群就是一个生成函数的问题了,答案就是[(x+1)^s1-1] * [(x+1)^s2-1] * ... * [(x+1)^sm-1],该多项式的第 k +1 项的系数,也就是 x ^k 的系数。最多可能有 n 个多项式相乘,可能有 n 项,那么如果使用普通的 FFT 算法在时间上是过不去了,必须要使用分治来做进一步优化。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 4e5 + 100;
const int maxm = 1e6 + 10;
const LL mod = 998244353LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; }
const int g = 3;
LL qp[40], fact[maxn>>2], inv[maxn>>2]; LL fast_pow(LL a, LL n){
LL res = 1;
while(n){
if(n&1) res = res * a % mod;
n >>= 1;
a = a * a % mod;
}
return res;
} void init(){
for(int i = 0; i < 30; ++i)
qp[i] = fast_pow(g, (mod-1)/(1<<i));
fact[0] = fact[1] = inv[0] = inv[1] = 1;
for(int i = 2; i <= n; ++i)
fact[i] = fact[i-1] * i % mod;
inv[n] = fast_pow(fact[n], mod - 2);
for(int i = n-1; i > 1; --i) inv[i] = inv[i+1] * (i+1) % mod;
} void rader(vector<LL> &F, int len){
int j = len >> 1;
for(int i = 1; i < len-1; ++i){
if(i < j) swap(F[i], F[j]);
int k = len >> 1;
while(j >= k){
j ^= k; k >>= 1;
}
if(j < k) j |= k;
}
} void NTT(vector<LL> &F, int len, int t){
int id = 0;
rader(F, len);
for(int h = 2; h <= len; h <<= 1){
++id;
for(int j = 0; j < len; j += h){
LL E = 1;
for(int k = j; k < j+h/2; ++k){
LL u = F[k];
LL v = E * F[k+h/2] % mod;
F[k] = (u + v) % mod;
F[k+h/2] = (u - v + mod) % mod;
E = E * qp[id] % mod;
}
}
}
if(t == -1){
for(int i = 1; i < (len>>1); ++i) swap(F[i], F[len-i]);
LL inv = fast_pow(len, mod - 2);
for(int i = 0; i < len; ++i) F[i] = F[i] * inv % mod;
}
} inline LL C(int n, int m){
return fact[n] * inv[m] % mod * inv[n-m] % mod;
} vector<LL> solve(int len1, int len2, vector<LL> &A, vector<LL> &B){
int len = 1;
while(len < (len1<<1) || len < (len2<<1)) len <<= 1;
A.resize(len); B.resize(len);
NTT(A, len, 1);
NTT(B, len, 1);
for(int i = 0; i < len; ++i) A[i] = A[i] * B[i] % mod;
NTT(A, len, -1);
while(len > 1 && A[len-1] == 0) --len;
A.resize(len);
return A;
} vector<LL> sum[maxn];
vector<vector<LL> > all; void dfs(int l, int r, int rt){
if(l == r){ sum[rt] = all[l]; return ; }
int m = l + r >> 1;
dfs(lson); dfs(rson);
sum[rt] = solve(sum[rt<<1].sz, sum[rt<<1|1].sz, sum[rt<<1], sum[rt<<1|1]);
} int main(){
int k;
scanf("%d %d %d", &n, &m, &k);
init(); all.pb(vector<LL>());
for(int i = 0; i < m; ++i){
int x; scanf("%d", &x);
vector<LL> v; v.pb(0);
for(int j = 1; j <= x; ++j) v.pb(C(x, j));
all.pb(v);
}
dfs(1, m, 1);
printf("%lld\n", sum[1][k]);
return 0;
}

  

挑选队友 (生成函数 + FFT + 分治)的更多相关文章

  1. loj6570 毛毛虫计数(生成函数FFT)

    link 巨佬olinr的题解 <-- olinr很强 考虑生成函数 考虑直径上点数>=4的毛毛虫的直径,考虑直径中间那些节点以及他上面挂的那些点的EGF \(A(x)=\sum_{i\g ...

  2. hdu5197 DZY Loves Orzing(FFT+分治)

    hdu5197 DZY Loves Orzing(FFT+分治) hdu 题目描述:一个n*n的矩阵里填入1~n^2的数,要求每一排从前往后能看到a[i]个数(类似于身高阻挡视线那种),求方案数. 思 ...

  3. hdu5322 Hope(dp+FFT+分治)

    hdu5322 Hope(dp+FFT+分治) hdu 题目大意:n个数的排列,每个数向后面第一个大于它的点连边,排列的权值为每个联通块大小的平方,求所有排列的权值和. 思路: 考虑直接设dp[i]表 ...

  4. 牛客 133D 挑选队友 (分治FFT)

    大意: $n$个人, 分别属于$m$个组, 要求选出$k$个人, 使得每组至少有一人, 求方案数. 显然答案为$\prod((1+x)^{a_i}-1)$的第$k$项系数, 分治$FFT$即可. #i ...

  5. [题解] Atcoder ABC 225 H Social Distance 2 生成函数,分治FFT

    题目 首先还没有安排座位的\(m-k\)个人之间是有顺序的,所以先把答案乘上\((m-k)!\),就可以把这些人看作不可区分的. 已经确定的k个人把所有座位分成了k+1段.对于第i段,如果我们能求出这 ...

  6. LOJ2541 PKUWC2018 猎人杀 期望、容斥、生成函数、分治

    传送门 首先,每一次有一个猎人死亡之后\(\sum w\)会变化,计算起来很麻烦,所以考虑在某一个猎人死亡之后给其打上标记,仍然计算他的\(w\),只是如果打中了一个打上了标记的人就重新选择.这样对应 ...

  7. jzoj6005. 【PKUWC2019模拟2019.1.17】数学 (生成函数+FFT+抽代+高精)

    题面 题解 幸好咱不是在晚上做的否则咱就不用睡觉了--都什么年代了居然还会出高精的题-- 先考虑如果暴力怎么做,令\(G(x)\)为\(F(n,k)\)的生成函数,那么不难发现\[G^R(x)=\pr ...

  8. CodeForces 553E Kyoya and Train 动态规划 多项式 FFT 分治

    原文链接http://www.cnblogs.com/zhouzhendong/p/8847145.html 题目传送门 - CodeForces 553E 题意 一个有$n$个节点$m$条边的有向图 ...

  9. 【BZOJ3771】Triple 生成函数 FFT 容斥原理

    题目大意 有\(n\)把斧头,不同斧头的价值都不同且都是\([0,m]\)的整数.你可以选\(1\)~\(3\)把斧头,总价值为这三把斧头的价值之和.请你对于每种可能的总价值,求出有多少种选择方案. ...

随机推荐

  1. Spring再接触 整合Hibernate

    首先更改配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  2. dbForge Studio for MySQL 中文乱码问题

    设置一下编码格式就好了 第一步:右键点击连接的数据库,选择第二个 第二步:选择第二个选项卡,设置编码格式,点击OK 第三步:确认保存并从新连接 最后你就发现能正常显示中文啦

  3. 大数据架构工具hadoop

    Hadoop是一个开源框架,它允许在整个集群使用简单编程模型计算机的分布式环境存储并处理大数据.它的目的是从单一的服务器到上千台机器的扩展,每一个台机都可以提供本地计算和存储. “90%的世界数据在过 ...

  4. Linux 用户与组

    在 Linux 操作系统下,如何添加一个新用户到一个特定的组中?如何同时将用户添加到多个组中?又如何将一个已存在的用户移动到某个组或者给他增加一个组?对于不常用 Linux 的人来讲,记忆 Linux ...

  5. weui复选框无法传值

    //原来的反了 function changeState (ele) { if(ele.checked){ ele.setAttribute('checked','checked') console. ...

  6. webpack工程搭建

    >>建立nodejs工程 新建文件夹,npm init 生成package.json >>安装webpack 和 webpack-dev-server npm install ...

  7. 再次认识void

    重新认识void 在初学c/c++时感觉void是一个很不起眼的关键字.因为在c++中我使用的还是比较少的.但是到了Linux中,不论是在内核源码中还是在程序编写的过程中有关void与*的组合随处可见 ...

  8. 配置java-jdk

    我将jdk安装在 /usr/local/jdk目录下 安装好之后 cd /etc/ ls vim profile export JAVA_HOME=/usr/jdk1.8.0_121  #你的jdk所 ...

  9. redis各类错误可能的解决方案

    .报Timeout performing EVAL 这个可能是连到读库从库了,直接改成写库就没问题了. 2. 各种ConnectTimeout 一般是偶尔或经常就超时,这种情况,找了各种回答,最后在s ...

  10. DynamoDB

    https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html Creating a  New  Table I ...