线段树+卷积

这个东西直接算不太好,但是合并两段结果却很方便,假设c[i]表示选i个数乘积的和,那么$a[i]=\sum_{j=0}^{i}{b[j]*c[i-j]}$

线段树维护即可

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + ;
const double pi = acos(-);
int n, len, ans, suma, sumb, mx = -1e9, m;
struct data {
double x, y;
data() {}
data(double _x, double _y) : x(_x), y(_y) {}
data friend operator - (const data &a, const data &b) {
return data(a.x - b.x, a.y - b.y);
}
data friend operator + (const data &a, const data &b) {
return data(a.x + b.x, a.y + b.y);
}
data friend operator * (const data &a, const data &b) {
return data(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
} a[N], b[N];
void fft(data *a, int len, int f) {
int n = << len;
for(int i = ; i < n; ++i) {
int t = ;
for(int j = ; j < len; ++j) {
if(i & ( << j)) {
t |= << (len - j - );
}
}
if(i < t) {
swap(a[i], a[t]);
}
}
for(int l = ; l <= n; l <<= ) {
int m = l >> ;
data w = data(cos(pi / m), f * sin(pi / m));
for(int i = ; i < n; i += l) {
data t = data(, );
for(int k = ; k < m; ++k, t = t * w) {
data x = a[k + i], y = t * a[i + k + m];
a[k + i] = x + y;
a[i + m + k] = x - y;
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) {
scanf("%lf", &a[i].x);
suma += a[i].x;
}
for(int i = ; i <= n; ++i) {
scanf("%lf", &b[i].x);
b[i + n].x = b[i].x;
sumb += b[i].x;
}
int c = floor((double)(sumb - suma) / n + 0.5);
for(int i = ; i <= n; ++i) {
a[i].x += c;
ans += a[i].x * a[i].x + b[i].x * b[i].x;
}
reverse(b + , b + * n + );
for(; << len <= * n; ++len);
fft(a, len, );
fft(b, len, );
for(int i = ; i < << len; ++i) {
a[i] = a[i] * b[i];
}
fft(a, len, -);
for(int i = n + ; i <= * n + ; ++i) {
a[i].x /= ( << len);
mx = max(mx, (int)(a[i].x + 0.1));
}
printf("%d\n", ans - * mx);
return ;
}

bzoj2962的更多相关文章

  1. 【BZOJ2962】序列操作(线段树)

    [BZOJ2962]序列操作(线段树) 题面 BZOJ 题解 设\(s[i]\)表示区间内选择\(i\)个数的乘积的和 考虑如何向上合并? \(s[k]=\sum_{i=0}^klson.s[i]*r ...

  2. 【BZOJ2962】序列操作 线段树

    [BZOJ2962]序列操作 Description 有一个长度为n的序列,有三个操作1.I a b c表示将[a,b]这一段区间的元素集体增加c,2.R a b表示将[a,b]区间内所有元素变成相反 ...

  3. [bzoj2962]序列操作_线段树_区间卷积

    序列操作 bzoj-2962 题目大意:给定一个n个数的正整数序列,m次操作.支持:1.区间加:2.区间取相反数:3.区间求选c个数的乘积和. 注释:$1\le n,m\le 5\cdot 10^4$ ...

  4. 【BZOJ-2962】序列操作 线段树 + 区间卷积

    2962: 序列操作 Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 678  Solved: 246[Submit][Status][Discuss] ...

  5. bzoj2962 序列操作 题解

    题目大意: 有一个长度为n的序列,有三个操作1.I a b c表示将[a,b]这一段区间的元素集体增加c,2.R a b表示将[a,b]区间内所有元素变成相反数,3.Q a b c表示询问[a,b]这 ...

  6. 序列操作 BZOJ2962 线段树

    分析: 数据范围表示:c特别的小(c<20) 我们可以考虑nlogn*c^2的算法. 线段树维护区间信息:f[i]表示在[l,r]这段区间中选择i个数相乘的和. 因此,我们可以将区间看成一个点, ...

  7. 序列操作bzoj2962(未完成)

    题解: 注意到k<=20;记录20个数f1...f20表示从选1-20个数的乘积分别为多少, 那么是一个(…+x)(…+x)……(…+x) 拆括号,枚举含有j个x,那么就是xj∗f[i−j]∗C ...

  8. 2019.01.04 bzoj2962: 序列操作(线段树+组合数学)

    传送门 线段树基础题. 题意:要求维护区间区间中选择ccc个数相乘的所有方案的和(c≤20c\le20c≤20),支持区间加,区间取负. 由于c≤20c\le20c≤20,因此可以对于每个线段树节点可 ...

  9. bzoj2962 序列操作

    2962: 序列操作 Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 1145  Solved: 378[Submit][Status][Discuss ...

  10. [BZOJ2962][清华集训]序列操作

    bzoj luogu 题意 有一个长度为\(n\) 的序列,有三个操作: \(I \ \ a\ b\ c\ :\)表示将\([a,b]\)这一段区间的元素集体增加\(c\): \(R \ \ a\ b ...

随机推荐

  1. 为公司做crm资产管理

    一.实现会议室预定 二.实现调查问卷 三.项目背景初始化分析 四.简单的登陆注册 五.学生管理 六.老师管理 七.销售管理 八.客户关系管理 九.抢单管理 十.微信发消息发邮件管理 补充:数据表设计. ...

  2. 关于error:Cannot assign to &#39;self&#39; outside of a method in the init family

    有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写.在这种方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息例如以下:error:C ...

  3. 微信小程序 如何使用globalData

    微信小程序在JavaScript文件中声明的变量和函数只在该文件中有效:不同的文件中可以声明相同名字的变量和函数,不会互相影响.如果希望在各个页面之间共同使用某些信息,并且可以对共享数据进行修改设置, ...

  4. Appium python自动化测试系列之Capability介绍(五)

    ​5.1 Capability介绍 5.1.1 什么是Capability 在讲capability之前大家是否还记得在讲log时给大家看过的启动时的日志?在我们的整个启动日志中会出现一些配置信息,其 ...

  5. android arcmenu

    http://www.kankanews.com/ICkengine/archives/129193.shtml

  6. SICP 习题 (1.38)解题总结

    SICP 习题1.38 紧跟着习题1.37的方向,要求我们用习题1.37中定义的cont-frac过程计算数学家欧拉大师在论文De Fractionibus Continuis 中提到的e-2的连分式 ...

  7. LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

    1 什么是“module machine type” 这个是当前工程要链接的静态库的target machine type. 2 什么是“target machine type” 这个是当前工程生成的 ...

  8. Fully qualified domain name for gitlab

    nginx - Fully qualified domain name for gitlab - Stack Overflow https://stackoverflow.com/questions/ ...

  9. React深入源码--了解Redux用法之Provider

    在Redux中最核心的自然是组件,以及组件相关的事件与数据流方式.但是我们在Redux中并没有采用传统的方式在getInitialState()中去初始化数据,而是采用Provider统一处理,省去了 ...

  10. Mac 操作系统安装 SVN server教程(Subversion With Mac OS X Tutorial)

    Find recent articles on my github page: rubyrobot.github.io © 2006-2014 Imagine Ecommerce Subversion ...