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 好好的一道模板题,我自己被自己坑了好久.. 首先题目看错.......什么玩意.......首 ...
随机推荐
- [USACO2009 OPEN] 滑雪课 Ski Lessons
洛谷P2948 看到题目就觉得这是动规但一直没想到如何状态转移……看了别人的题解之后才有一些想法 f[i][j]:前i单位时间能力值为j可以滑的最多次数 lessons[i][j]:结束时间为i,获得 ...
- Python---协程---重写多线程
一. # 用协程的方式,修改播放电影和音乐的练习题 # 用协程的方式完成播放 movie_list = ["斗破.mp4", "复仇者联盟.avi", &quo ...
- Angular前端开源框架
1. 项目框架介绍 本项目采用了开源项目ngx-admin作为脚手架.基于Angular 8 +,Bootstrap 4+和Nebular,ngx-admin是最受欢迎的后台管理模板,是用于个人和商 ...
- docker 安装MongoDB以及设置用户
MongoDB 是一个免费的开源跨平台面向文档的 NoSQL 数据库程序. 1.查看可用的 MongoDB 版本 访问 MongoDB 镜像库地址: https://hub.docker.com/_/ ...
- sh_01_判断年龄
sh_01_判断年龄 # 1. 定义一个整数变量记录年龄 age = 15 # 2. 判断是否满了18岁 if age >= 18: # 3. 如果满了18岁,可以进网吧嗨皮 print(&qu ...
- window 下 Atom 侧边栏字体大小设置
在 File 处找到 Settings 点击 找到 Themes 点击 找到 your stylesheet 点击 在 .tree-view 处设置即可, (按照 css 样式来写即可保存生效).
- 蒙特卡洛(Monte Carlo)方法求面积
如图,刷微博时,看到一个问题,第一个想到的就是用蒙特卡洛方法求解,当时正在练python,于是尝试用python编写程序. import random # 先求s1 k=0 n=100000000 f ...
- spring-boot-devtools热加载不起作用
在开发过程中,希望修改时能够及时更新修改,即热加载,但是spring-boot-devtools不起作用.这主要是两个原因导致. 一.spring-boot-maven-plugin插件没有配置,如下 ...
- gsxt滑动验证码
最后,谈谈滑动验证码. 目前,工商网站已经全面改版,全部采用了滑动验证码,上面绝大多数思路都失效了.对于滑动验证码,网上能搜到的解决方案基本都是下载图片,还原图片,算出滑动距离,然后模拟js来进行拖动 ...
- Python编程:从入门到实践—类
创建类 #!/usr/bin/env python# --*-- encoding:utf-8 --*-- class Dog(): """一次模拟小狗的简单尝试&quo ...