SCUT - 271 - CC 非诚勿扰 - FFT
https://scut.online/p/271
第一次遇到没这么裸的,其实感觉到是卷积但是不知道怎么化。看来以后要多注意下标。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e6;
const double PI = acos(-1.0);
struct Complex {
double x, y;
Complex() {}
Complex(double x, double y): x(x), y(y) {}
friend Complex operator+(const Complex &a, const Complex &b) {
return Complex(a.x + b.x, a.y + b.y);
}
friend Complex operator-(const Complex &a, const Complex &b) {
return Complex(a.x - b.x, a.y - b.y);
}
friend Complex operator*(const Complex &a, const Complex &b) {
return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
} A[MAXN + 5], B[MAXN + 5];
void FFT(Complex a[], int n, int op) {
for(int i = 1, j = n >> 1; i < n - 1; ++i) {
if(i < j)
swap(a[i], a[j]);
int k = n >> 1;
while(k <= j) {
j -= k;
k >>= 1;
}
j += k;
}
for(int len = 2; len <= n; len <<= 1) {
Complex wn(cos(2.0 * PI / len), sin(2.0 * PI / len)*op);
for(int i = 0; i < n; i += len) {
Complex w(1.0, 0.0);
for(int j = i; j < i + (len >> 1); ++j) {
Complex u = a[j], t = a[j + (len >> 1)] * w ;
a[j] = u + t, a[j + (len >> 1)] = u - t;
w = w * wn;
}
}
}
if(op == -1) {
for(int i = 0; i < n; ++i)
a[i].x = (int)(a[i].x / n + 0.5);
}
}
int pow2(int x) {
int res = 1;
while(res < x)
res <<= 1;
return res;
}
void convolution(Complex A[], Complex B[], int Asize, int Bsize) {
int n = pow2(Asize + Bsize - 1);
for(int i = 0; i < n; ++i) {
A[i].y = 0.0;
B[i].y = 0.0;
}
for(int i = Asize; i < n; ++i)
A[i].x = 0;
for(int i = Bsize; i < n; ++i)
B[i].x = 0;
FFT(A, n, 1);
FFT(B, n, 1);
for(int i = 0; i < n; ++i)
A[i] = A[i] * B[i];
FFT(A, n, -1);
return;
}
ll ans[MAXN+5],ans0;
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i) {
scanf("%lf", &A[i].x);
}
for(int i = 0; i < n; ++i) {
scanf("%lf", &B[i].x);
}
reverse(B,B+n);
convolution(A, B, n, n);
for(int i=0;i<(n<<1);++i){
ans[i]=(ll)round(A[i].x);
//printf("%lld\n",ans[i]);
}
while(m--){
int k;
scanf("%d",&k);
if(k==0)
printf("%lld\n",ans[n-1]);
else
printf("%lld\n",ans[n-1-k]+ans[n-1+k]);
}
return 0;
}
SCUT - 271 - CC 非诚勿扰 - FFT的更多相关文章
- SCUT - 354 - CC的简单多项式 - 杜教筛
https://scut.online/p/354 跟多项式一点关系都没有. 注意到其实两个多项式在1处求值,那么就是他们的系数加起来. 列一列发现系数就是n以内两两求gcd的值,还自动把0去掉了. ...
- SCUT - 157 - CC和他的GCD - 容斥原理
https://scut.online/p/157 鉴于多年(都没几个月)搞数论的经验,这种时候枚举g肯定是对的. 那么肯定是要莫比乌斯函数作为因子,因为很显然? 但是为什么要搞个负的呢?其实是因为这 ...
- SCUT - 274 - CC B-Tree - 树形dp
https://scut.online/p/274 首先要判断是一颗树,并且找出树的直径. 是一棵树,首先边恰好有n-1条,其次要连通,这两个条件已经充分了,当然判环可以加速. 两次dfs找出直径,一 ...
- Codeforces 993E Nikita and Order Statistics [FFT]
洛谷 Codeforces 思路 一开始想偏想到了DP,后来发现我SB了-- 考虑每个\(a_i<x\)的\(i\),记录它前一个和后一个到它的距离为\(L_i,R_i\),那么就有 \[ an ...
- 【BZOJ 4332】 4332: JSOI2012 分零食 (FFT+快速幂)
4332: JSOI2012 分零食 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 119 Solved: 66 Description 这里是欢乐 ...
- Nginx(三):Linux环境(Ubuntu)下Nginx的安装
Nginx 是一位俄罗斯人 Igor Sysoev(伊戈尔·塞索斯夫)编写的一款高性能HTTP和反向代理服务器. Nginx 主要是有C编写的,安装Nginx需要GCC编译器(GNU Compiler ...
- CC countari & 分块+FFT
题意: 求一个序列中顺序的长度为3的等差数列. SOL: 对于这种计数问题都是用个数的卷积来进行统计.然而对于这个题有顺序的限制,不好直接统计,于是竟然可以分块?惊为天人... 考虑分块以后的序列: ...
- CC Arithmetic Progressions (FFT + 分块处理)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题目:给出n个数,选出三个数,按下标顺序形成等差数 ...
- 【BZOJ】3527: [Zjoi2014]力(fft+卷积)
http://www.lydsy.com/JudgeOnline/problem.php?id=3527 好好的一道模板题,我自己被自己坑了好久.. 首先题目看错.......什么玩意.......首 ...
随机推荐
- k-means原理和python代码实现
k-means:是无监督的分类算法 k代表要分的类数,即要将数据聚为k类; means是均值,代表着聚类中心的迭代策略. k-means算法思想: (1)随机选取k个聚类中心(一般在样本集中选取,也可 ...
- js-点出弹框后(除了点击窗口上的叉子),点其他地方能够关闭窗口???
HTML: <div class="A"> <div class="B"></div> </div> JS: $ ...
- SQL server int 转char类型
CONVERT(CHAR,c.battery_board_id) CONVERT(VARCHAR,c.battery_board_id)
- [USACO17JAN]Promotion Counting 题解
前言 巨佬说:要有线段树,结果蒟蒻打了一棵树状数组... 想想啊,奶牛都开公司当老板了,我还在这里码代码,太失败了. 话说奶牛开个公司老板不应该是FarmerJohn吗? 题解 刚看到这道题的时候竟然 ...
- k8s从Harbor拉取启动镜像测试
登陆harbor [root@k8s-master ~]# docker login 192.168.180.105:1180 Username: admin Password: WARNING! Y ...
- lists.newarraylist()和new arraylist() 区别
转自 https://blog.csdn.net/qq_2300688967/article/details/79490345 lists.newarraylist(): List<String ...
- curl_init raw传递json参数
protected function curl_vm_record($url, $platform, $authorization, $jsonStr) { $ch = curl_init(); cu ...
- 图论&线性基(?)(8.12)
边没有负权,最短路最多只有n条边 很暴力的思想: 先跑一遍最短路,找出最短路上的边,枚举每条边,翻倍,放进原图再跑一遍.取最大值 好熟悉啊 分层建图,建k层 每层内部是原图 若原图中u到v有连边,则由 ...
- 二十三、python中的time和datetime模块
A.time模块 1. sleep():强制等待 import timeimport datetime print("start to sleep.....")time.sle ...
- CGI FastCGI WSGI 解析
我们将服务端程序分为了web服务器和应用程序服务器. web服务器是用于处理HTML文件,让客户可以通过浏览器进行访问.主流的有apache,IIS,nginx,lghttpd等. 应用服务器处理业务 ...