题目链接

Emma is really fond of integers and loves playing with them. Her friends were jealous, and to test her, one of them gave her a problem. 
Emma is given a list A of N integers and is asked a set of Q queries. Each query is denoted by an integer K, for which you have to return the sum of product of all possible sublists having exactly K elements. 
Emma has got stuck in this problem and you being her best friend have decided to help her write a code to solve it. Since the answers can be very large, print the answers modulo100003.

Input Format
First line has an integer N, denoting the number of integers in list A. Next line contains N space separated integers. The third line contains integer Q, and next Q lines have a single integer K.

Output Format
For each of the queries, print the corresponding answer in a new line.

NOTE Sublist here refers to selecting K elements from a list of N elements. There will be (NK) ways to do that, it doesn't matter if two elements are same.

Constraints
1≤N≤3×104 
1≤Ai≤105 
1≤Q≤N 
1≤K≤N

Sample Input #00

3
1 2 3
2
1
2

Sample Output #00

6
11

Sample Input #01

3
1 2 2
1
2

Sample Output #01

8

Explanation

Sample #00: 
For K=1 possible sublists are {1},{2},{3} so answer is 1+2+3=6. 
For K=2 possible sublists are {1,2},{2,3},{3,1} so answer is (1×2)+(2×3)+(3×1)=2+6+3=11.

Sample #01: 
For K=2 possible sublists are {1,2},{2,2},{2,1} so answer is (1×2)+(2×2)+(2×1)=2+4+2=8.

题意:给出n个数,有q次询问,每次询问一个k,从n个数中选出k个数,对这k个数做乘积,求所有可能的选法的和。

我们把每个数看做一个多项式:x + A[i], 那么就可以得到n个这样的多项式。 将这n个多项式相乘,

那么k对应的查询的答案就是多项式中x^k项的系数,和母函数有点类似。

下面就是如何计算n个多项式的乘积,想到用FFT,但是不可以直接线性的计算这n个多项式的乘积,时间复杂度

太高,所以想到分治的思想,总的复杂度就是O(n*(log(n)^2))

Accepted Code:

 #define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <bitset>
#include <complex>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; } typedef long double Num; //??????long double?????
const Num PI = .141592653589793238462643383279L;
typedef complex<Num> Complex;
//n?????
//a?????
void fft_main(int n, Num theta, Complex a[]) {
for(int m = n; m >= ; m >>= ) {
int mh = m >> ;
Complex thetaI = Complex(, theta);
rep(i, mh) {
Complex w = exp((Num)i*thetaI);
for(int j = i; j < n; j += m) {
int k = j + mh;
Complex x = a[j] - a[k];
a[j] += a[k];
a[k] = w * x;
}
}
theta *= ;
}
int i = ;
reu(j, , n-) {
for(int k = n >> ; k > (i ^= k); k >>= ) ;
if(j < i) swap(a[i], a[j]);
}
} void fft(int n, Complex a[]) { fft_main(n, * PI / n, a); }
void inverse_fft(int n, Complex a[]) { fft_main(n, - * PI / n, a); } void convolution(vector<Complex> &v, vector<Complex> &w) {
int n = , vwn = v.size() + w.size() - ;
while(n < vwn) n <<= ;
v.resize(n), w.resize(n);
fft(n, &v[]);
fft(n, &w[]);
rep(i, n) v[i] *= w[i];
inverse_fft(n, &v[]);
rep(i, n) v[i] /= n;
} const int MOD = ; vector<int> calc_dfs(const vector<int> &A, int l, int r) {
if(r - l == ) {
vector<int> res();
res[] = ;
res[] = A[l];
return res;
}
int mid = (l + r) / ;
vector<int> L = calc_dfs(A, l, mid), R = calc_dfs(A, mid, r);
vector<Complex> Lc(all(L)), Rc(all(R));
convolution(Lc, Rc);
int n = L.size() + R.size() - ;
vector<int> res(n);
rep(i, n) res[i] = (long long)(Lc[i].real() + .) % MOD;
// cerr << "["<< l << "," << r <<"): ";
// rep(i, n) cerr << res[i] << ", "; cerr << endl;
return res;
} int main() {
int N;
scanf("%d", &N);
vector<int> A(N);
rep(i, N) {
scanf("%d", &A[i]);
A[i] %= MOD;
}
vector<int> ans = calc_dfs(A, , N);
int Q;
scanf("%d", &Q);
rep(ii, Q) {
int K;
scanf("%d", &K);
printf("%d\n", ans[K]);
}
return ;
}

Hackerrank--Emma and sum of products (FFT)的更多相关文章

  1. [CF1519D] Maximum Sum of Products (暴力)

    题面 有两个长为 n n n 的序列 a a a 和 b b b,至多反转 a a a 的一个子区间,最大化 ∑ i = 1 n a i ⋅ b i \sum_{i=1}^na_i\cdot b_i ...

  2. 1305 Pairwise Sum and Divide(数学 ,规律)

    HackerRank   1305 Pairwise Sum and Divide   有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = ...

  3. bzoj 3513 [MUTC2013]idiots FFT 生成函数

    [MUTC2013]idiots Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 806  Solved: 265[Submit][Status][Di ...

  4. HDU - 5307 :He is Flying (分治+FFT)(非正解)

    JRY wants to drag racing along a long road. There are nn sections on the road, the ii-th section has ...

  5. Fast Fourier Transform

    写在前面的.. 感觉自己是应该学点新东西了.. 所以就挖个大坑,去学FFT了.. FFT是个啥? 挖个大坑,以后再补.. 推荐去看黑书<算法导论>,讲的很详细 例题选讲 1.UOJ #34 ...

  6. SQL Server(三):Select语句

      1.最基本的Select语句: Select [Top n [With Ties]] <*|Column_Name [As <Alias>][, ...n]> From & ...

  7. cf #365b 巧妙的统计

     Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  8. 用 Python 通过马尔可夫随机场(MRF)与 Ising Model 进行二值图降噪

    前言 这个降噪的模型来自 Christopher M. Bishop 的 Pattern Recognition And Machine Learning (就是神书 PRML……),问题是如何对一个 ...

  9. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

随机推荐

  1. 2.初始化spark

    参考:  RDD programming guide http://spark.apache.org/docs/latest/rdd-programming-guide.html  SQL progr ...

  2. 介绍一下再Apache下的Tomcat负载均衡的一些使用问题

    在负载均衡技术中,硬件设备是比较昂贵的,对于负载均衡的学习者如果不是在企业中应用或者是学员中学习,很少有机会能碰到实际操作的训练.(http://xz.8682222.com)所以,很多朋友都会选择软 ...

  3. 21分钟教会你分析MaxCompute账单

    背景 阿里云大计算服务MaxCompute是一款商业化的大数据分析平台,其计算资源有预付费和后付费两种计费方式.并且产品每天按照project为维度进行计量计费(账单基本情况下会第二天6点前产出).本 ...

  4. F. Cowmpany Cowmpensation dp+拉格朗日插值

    题意:一个数,每个节点取值是1-d,父亲比儿子节点值要大,求方案数 题解:\(dp[u][x]=\prod_{v}\sum_{i=1}^xdp[v][i]\),v是u的子节点,先预处理出前3000项, ...

  5. BZOJ2741:[FOTILE模拟赛]L

    Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...

  6. LUOGU P4281 [AHOI2008]紧急集合 / 聚会 (lca)

    传送门 解题思路 可以通过手玩或打表发现,其实要选的点一定是他们三个两两配对后其中一对的$lca$上,那么就直接算出来所有的$lca$,比较大小就行了. #include<iostream> ...

  7. pytorch 入门指南

    两类深度学习框架的优缺点 动态图(PyTorch) 计算图的进行与代码的运行时同时进行的. 静态图(Tensorflow <2.0) 自建命名体系 自建时序控制 难以介入 使用深度学习框架的优点 ...

  8. webpack 配置分离css插件

    以css配置示例,less与sass同理 1. 使用旧版的ExtractTextPlugin插件 安装 npm install extract-text-webpack-plugin@next --s ...

  9. 15_K-近邻算法之入住位置预测

    案例:本次大赛的目的是预测一个人想签入到哪个地方.对于本次比赛的目的,Facebook的创建一 个人造的世界,包括位于10公里的10平方公里超过10万米的地方.对于一个给定的坐标,你的任务是返回最有可 ...

  10. 从0开始学习ssh之岗位管理

    岗位应该有如下属性,id.名称.说明.新建role.java.添加id,name,description并增加三个的get set方法. 之后建立Role.hbm.xml 并加入到applicatio ...