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 好好的一道模板题,我自己被自己坑了好久.. 首先题目看错.......什么玩意.......首 ...
随机推荐
- [每日一讲] Python系列:数字与运算符
数字(数值)型 Python 数字数据类型用于存储数值.数据类型是不可变(immutable)的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. Python 支持三种不同的数值类型: 整型 ...
- 用TweenMax.js动画让数字动起来
html: <div class="val2">0</div> js: let val2 = document.getElementsByClassName ...
- HTML5基础内容(二)
HTML(HyperText Markup Language)超文本标记语言 一.HTML注释 元素就是标签,标签就是元素. 注释中的内容不会在页面中显示,但是可以在源码中看到. 可以通过编写注释来对 ...
- Bugku web 计算器
计算器 打开网页,想输入正确的计算结果发现只输进去一位数??? 遇事不决先F12看一眼源码,发现flag
- BZOJ 3357: [Usaco2004]等差数列 动态规划
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- Apache启动错误:could not bind to address[::]:443
Q:Windows环境下启动apache报错如下: 可是在httpd.conf文件中apache listen的明明是http 80端口,为什么会报443的错误? A:因为你的计算机安装了VM,所有有 ...
- 【noip2016提高组day2T3】【愤怒的小鸟】状压dp转移时的集合包含
(上不了p站我要死了,图来自百度,侵权度娘背锅) 调死我了... 标题就说明了,死在了集合包含上.因为这道题与其他的状压题不同,其他的题基本上都是要求集合不重合,而这道题完全是可以的. 废话不多说,先 ...
- linux入门 一些常见命令
chvt 切换终端 用法 1是终端 7是图形界面 sudo chvt 1 or 7 cp 复制文件/目录 -a : 通常在复制目录时使用,保存链接,文件属性并递归复制目录 -f:若文件在目标路径中则强 ...
- 【bzoj1096】[ZJOI2007]仓库建设
*题目描述: L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总 ...
- Leetcode 2. Add Two Numbers(指针和new的使用)结构体指针
---恢复内容开始--- You are given two non-empty linked lists representing two non-negative integers. The di ...