4827: [Hnoi2017]礼物

链接

分析:

  求最小的$\sum_{i=1}^{n}(x_i-y_i)^2$

  设旋转了j位,每一位加上了c。

  $\sum\limits_{i=1}^{n}(x_{i+j}+c-y_i)^2$

  $=\sum\limits_{i=1}^{n}x_{i+j}^2+y_i^2+c^2+2x_{i+j}c-2y_ic-2x_{i+j}y_i$

  $=\sum x_i^2+\sum y_i^2+nc^2+2c \sum (x_i-y_i)-2\sum x_{i+j}y_i$

  发现只有最后一项是要求的。

  $\sum x_{i+j}y_i$ 然后把y翻转一下,就是$\sum x_{i+j}y_{n - i +1}$,再把x加倍即可。

  考虑为什么这样就解决了,如果不旋转,那么$a[1],a[2],a[3]$与$b[1],b[2],b[3]$相乘,那么得到的$c[4]=a[1] \times b[3]+a[2]\times b[2]+a[3]\times b[1]$,所以如果翻转b,c[4]就是旋转0位的答案。在a后面加倍一次,得到那么f[5]就是旋转1位的答案,同理f[6]是旋转2位的答案。

  而且c是可以直接求出的,可以不用枚举,戳这

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = , INF = 1e9;
const double Pi = acos(-1.0), eps = 1e-;
int rev[N], f[N], x[N], y[N];
struct Com{
double x, y;
Com(double _x = 0.0,double _y = ) { x = _x, y = _y; }
}a[N], b[N];
Com operator + (const Com &A,const Com &B) { return Com(A.x + B.x, A.y + B.y); }
Com operator - (const Com &A,const Com &B) { return Com(A.x - B.x, A.y - B.y); }
Com operator * (const Com &A,const Com &B) { return Com(A.x * B.x - A.y * B.y, A.x * B.y + A.y * B.x); } void FFT(Com *a,int n,int ty) {
for (int i = ; i < n; ++i) if (i < rev[i]) swap(a[i], a[rev[i]]);
Com w1, w, t, u;
for (int m = ; m <= n; m <<= ) {
w1 = Com(cos( * Pi / m), ty * sin( * Pi / m));
for (int i = ; i < n; i += m) {
w = Com(, );
for (int k = ; k < (m >> ); ++k) {
t = w * a[i + k + (m >> )];
u = a[i + k];
a[i + k] = u + t;
a[i + k + (m >> )] = u - t;
w = w * w1;
}
}
}
}
void mul(Com *a,Com *b,int 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 = ; i <= len; ++i) f[i] = (int)(a[i].x / (double)len + 0.5);
}
int main() {
int n = read(), m = read(), len = , lg = , Sx = , Sy = , S = ;
while (len <= n + n) len <<= , lg ++;
for (int i = ; i <= len; ++i)
rev[i] = (rev[i >> ] >> ) | ((i & ) << (lg - ));
for (int i = ; i < n; ++i)
x[i] = read(), Sx += (x[i] * x[i]), S += * x[i];
for (int i = ; i < n; ++i)
y[i] = read(), Sy += (y[i] * y[i]), S -= * y[i];
for (int i = ; i < n + n; ++i) a[i] = Com(x[i % n], );
for (int i = ; i < n; ++i) b[i] = Com(y[n - i - ], );
mul(a, b, len);
int ans = INF;
for (int c = -m; c <= m; ++c)
for (int i = n; i < n + n; ++i)
ans = min(ans, Sx + Sy + c * S + n * c * c - * f[i]);
cout << ans;
return ;
}

4827: [Hnoi2017]礼物的更多相关文章

  1. bzoj 4827: [Hnoi2017]礼物 [fft]

    4827: [Hnoi2017]礼物 题意:略 以前做的了 化一化式子就是一个卷积和一些常数项 我记着确定调整值还要求一下导... #include <iostream> #include ...

  2. bzoj 4827 [Hnoi2017]礼物——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4827 式子就是 \sum_{i=0}^{n-1}(a[ i ] - b[ i+k ] + c ...

  3. bzoj 4827 [Hnoi2017] 礼物 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4827 首先,旋转对应,可以把 b 序列扩展成2倍,则 a 序列对应到的还是一段区间: 再把 ...

  4. bzoj 4827: [HNOI2017]礼物 (FFT)

    一道FFT 然而据说暴力可以水70分 然而我省选的时候看到了直接吓傻了  连暴力都没打 太弱了啊QAQ emmmm 详细的拆开就看其他题解吧233 最后那一步卷积其实我一直没明白 后来画画图终于懂了 ...

  5. BZOJ:4827: [Hnoi2017]礼物

    [问题描述] 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度. 但是在她生日的 ...

  6. 【刷题】BZOJ 4827 [Hnoi2017]礼物

    Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...

  7. BZOJ 4827 [Hnoi2017]礼物 ——FFT

    题目上要求一个循环卷积的最小值,直接破环成链然后FFT就可以了. 然后考虑计算的式子,可以分成两个部分分开计算. 前半部分FFT,后半部分扫一遍. #include <map> #incl ...

  8. bzoj 4827: [Hnoi2017]礼物【FFT】

    记得FFT要开大数组!!开到快MLE的那种!!我这个就是例子TAT,5e5都RE了 在这题上花的时间太多了,还是FFT不太熟练. 首先看70分的n方做法:从0下标开始存,先n--,把a数组倍增,然后枚 ...

  9. BZOJ 4827: [Hnoi2017]礼物 FFT_多项式_卷积

    题解稍后在笔记本中更新 Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r&q ...

随机推荐

  1. springMVC入门-02

    本节会在上节基础上讨论springMVC如何传值的问题. 在添加dispatcherServlet之后,拦截器会将url中的参数拦截下来,使之可以在controller中使用.以下代码就是在前台输入u ...

  2. [翻译] TCBlobDownload

    TCBlobDownload TCBlobDownload uses NSOperations to download large files (typically videos, music... ...

  3. 企业级实时数据文件同步服务_【all】

    全网数据定时备份方案[cron + rsync] [更多参考]全网数据定时备份方案[cron + rsync] 全网数据实时备份方案[inotify,sersync] [更多参考]全网数据实时备份方案 ...

  4. Linux系统重要的开机自启动的服务

    重要的开机自启动的服务 1.sshd  连接Linux服务器是需要用到的服务程序    2.rsyslog 操作日志的一种机制                系统日志:/var/log/message ...

  5. 【php】页面加载优化的14条原则

      1. 尽可能的减少 HTTP 的请求数 [content] 2. 使用 CDN(Content Delivery Network) [server] 3. 添加 Expires 头(或者 Cach ...

  6. September 08th 2017 Week 36th Friday

    Death is so terribly final, while life is full of possibilities. 死亡是冰冷可怕的绝境,而或者却充满了无限的可能. It isn't t ...

  7. Nodejs Redis 全部操作方法

    安装  npm install redis --save demo var redis = require('redis'); var client = redis.createClient('637 ...

  8. SuperMap/PlottingSymbol

    https://github.com/SuperMap/PlottingSymbol

  9. 学习python第四天——Oracle查询

    3.子查询(难): 当进行查询的时候,发现需要的数据信息不明确,需要先通过另一个查询得到, 此查询称为子查询: 执行顺序:先执行子查询得到结果以后返回给主查询 组成部分: 1).主查询部分 2).子查 ...

  10. centos6.2/6.3/6.4+nginx+mysql5.5+php5.3.14

    一.安装所需软件包yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype free ...