链接: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. DOM访问和处理HTML文档的标准方法

    innerHTML获取或替换HTML元素的内容 <p id="d">对于错我无法来判断 </p><script type="text/jav ...

  2. spring框架校验自定义注解

    起因: 项目开发时遇到一个问题是对于金额类型的字段,数据库中格式一般为BigDecimal类型,两位小数点,然后在接口定义中如果不定义成String类型的话,就不能使用@pattern注解限定格式,而 ...

  3. Taro开发小程序移动地图固定中间获取地址

    效果图如下: 绿色地标固定在中间,移动地图的时候获取对应信息. 先定义map. <Map className="location" id={location} latitud ...

  4. vue---mint-ui组件loadmore(上拉加载,下拉刷新)

    1. 先安装mint-ui 2. 在main.js中引入mint-ui的css样式和组件 import "mint-ui/lib/style.css"; import {Loadm ...

  5. js解决下拉列表框互斥选项的问题

    如图不区分选项与其他选项是互斥的关系,当选择了不区分时,其他选项就要去除,当有其他选项存在时,就不能有不区分 解决办法:定义change事件,若列表发生改变,首先判断点击的是否是不区分,若是,则将其他 ...

  6. linux应用之test命令详细解析

    test命令用法. 功能:检查文件和比较值 1)判断表达式 if test  (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2                  两个表 ...

  7. pythond的icmp广播报获取局域网主机IP

    icmp广播报获取局域网四川特产IP from scapy.all import *import randomimport threading def scan(sip,dip): pkt = Eth ...

  8. ./configure -build,-host,-target设置

    build:执行代码编译的主机,正常的话就是你的主机系统.这个参数一般由config.guess来猜就可以.当然自己指定也可以.host:编译出来的二进制程序所执行的主机,因为绝大多数是如果本机编译, ...

  9. Bean method 'jdbcTemplate' not loaded because @ConditionalOnSingleCandidate

    springboot学习jdbcTemplate操作数据库的过程中,出现这个问题 后来发现是由于程序中有配置下面这个注解 @EnableAutoConfiguration(exclude = {Dat ...

  10. vue-实现全选单选

    在获取列表页面数据时,通过forEach遍历存储数据的对象,给对象中添加一个selected变量,值为布尔值. 点击全选时,通过遍历将对象中selected的布尔值改变 点击单选时,被点中的通过筛选加 ...