大致题意:

给你N个整数和M个整数,问这M个数中,有几个数可以表达成那N个整数中一个或者两个整数的和。

分析:

算是半个裸的FFT。FFT可以用来在nlongn时间内求高精度乘法,我们先模拟一下乘法。

A4A3A2A1A0*B4B3B2B1B0  Ai,Bj表示位数,结果保存在Ck中

4   3   2   1   0(下标)

A4 A3 A2 A1 A0

B4 B3 B2 B1 B0

先不考虑进位

那么C0=A0*B0

C1=A0*B1+A1*B0

Ck=sum(Ai*Bj) (i+j=k)

我们现在看题目是求两个数或者一个数的和是否能在M中匹配到。与上式对比,发现了共同点,i,j的和的下标对应的值即Ck,也就是说我们可以把出现的数字当做位置,出现的位置赋值为1,没有为0,第0位为1,因为可以加0嘛。

样例如下

       5 4 3 2 1 0(下标)

       1 0 1 0 1 1

X            1 0 1 0 1 1

=  1 0 2 0 3 2 2 2 1 2 1

A 9 8 7 6 5 4 3 2 1 0(下标)

结果不需要再进位,只要k下标对应的值大于0就符合条件。

#include <bits/stdc++.h>
using namespace std; const double PI = acos(-1.0);
struct Complex
{
double x, y;
Complex(double _x = 0.0, double _y = 0.0)
{
x = _x;
y = _y;
}
Complex operator - (const Complex &b)const
{
return Complex(x-b.x, y-b.y);
}
Complex operator + (const Complex &b)const
{
return Complex(x+b.x, y+b.y);
}
Complex operator * (const Complex &b)const
{
return Complex(x*b.x-y*b.y, x*b.y+y*b.x);
}
}; void change(Complex y[], int len)
{
int i, j, k;
for(i = 1, j = len/2; i < len-1; i++)
{
if (i < j) swap(y[i], y[j]);
k = len/2;
while(j >= k)
{
j -= k;
k /= 2;
}
if (j < k) j += k;
}
} void fft(Complex y[], int len, int on)
{
change(y, len);
for(int h = 2; h <= len; h <<= 1)
{
Complex wn(cos(-on*2*PI/h), sin(-on*2*PI/h));
for(int j = 0; j < len; j += h)
{
Complex w(1, 0);
for(int k = j; k < j+h/2; k++)
{
Complex u = y[k];
Complex t = w*y[k+h/2];
y[k] = u+t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if (on == -1)
for(int i = 0; i < len; i++)
y[i].x /= len;
} const int maxn=4*200010;
Complex x1[maxn],x2[maxn];
int a[maxn/4];
int sum[maxn]; int main()
{
int N,M;
while(~scanf("%d",&N))
{
memset(a,0,sizeof(a));
int len1=0;
for(int i=0; i<N; i++)
{
int tmp;
scanf("%d",&tmp);
a[tmp]=1;
len1=max(len1,tmp);
}
len1++;
int len=1;
while(len < len1*2) len<<=1;
a[0]=1;
for(int i=0; i<len1; i++)
x1[i]=Complex(a[i],0);
for(int i=0; i<len1; i++)
x2[i]=Complex(a[i],0);
for(int i=len1; i<len; i++)
x2[i]=Complex(0,0);
fft(x1,len,1);
fft(x2,len,1);
for(int i=0; i<len; i++)
x1[i]=x1[i]*x2[i];
fft(x1,len,-1);
for(int i=0; i<len; i++)
sum[i]=(int)(x1[i].x+0.5);
scanf("%d",&M);
len=2*len1-1;
int cnt=0;
for(int i=0;i<M;i++)
{
int t;
scanf("%d",&t);
if(sum[t]>0) cnt++;
}
printf("%d\n",cnt);
}
return 0;
}

Gym 100783C Golf Bot FFT的更多相关文章

  1. UVALive 6886 Golf Bot FFT

    Golf Bot 题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129724 Description Do ...

  2. UVALIVE6886 Golf Bot (FFT)

    题意:打高尔夫 给你n个距离表示你一次可以把球打远的距离 然后对于m个询问 问能否在两杆内把球打进洞 题解:平方一下就好 注意一下x0的系数为1表示打一杆 才发现数组应该开MAXN * 4 之前写的题 ...

  3. LA6886 Golf Bot(FFT)

    题目 Source https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page= ...

  4. UVALive - 6886 Golf Bot 多项式乘法(FFT)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/129724 Golf Bot Time Limit: 15000MS 题意 给你n个数,m个查询,对于每个查询 ...

  5. HNU11376:Golf Bot

    Problem description Input The first line has one integer: N, the number of different distances the G ...

  6. Gym100783C Golf Bot(FFT)

    https://vjudge.net/problem/Gym-100783C 题意: 给出n个数,然后有m次查询,每次输入一个数x,问x能否由n个数中2个及2个以下的数相加组成. 思路:题意很简单,但 ...

  7. [Swerc2014 C]Golf Bot

    题意:给你N个数字,每次利用这N个数字中最多两个数字进行加法运算,来得到目标中的M个数字. Solution: 我们先来看看多项式乘法:\(A(x)=\sum_{i=0}^{n-1}a_ix^i\), ...

  8. FFT题集

    FFT学习参考这两篇博客,很详细,结合这看,互补. 博客一 博客二 很大一部分题目需要构造多项式相乘来进行计数问题. 1. HDU 1402 A * B Problem Plus 把A和B分别当作多项 ...

  9. Codeforces Gym 100803D Space Golf 物理题

    Space Golf 题目连接: http://codeforces.com/gym/100803/attachments Description You surely have never hear ...

随机推荐

  1. Linux_软件包管理基本概述

    一.回去软件包的途径 1.系统发行版的光盘或官方的服务器镜像站 http://mirrors.aliyun.com        //阿里云镜像站 http://mirrors.sohu.com    ...

  2. easyui datagrid 自定义单元格单击与双击事件(Day_38)

    $(function(){ $('#tableId').datagrid({//单击事件   onClickRow:function(rowIndex,rowData){  alert("单 ...

  3. l初识CSRF(跨站请求伪造)

    一 CSRF是什么 CSRF(Cross-site request forgery)跨站请求伪造,也被称为"One Click Attack"或者Session Riding,通常 ...

  4. 详细教程丨如何利用Rancher和Kong实现服务网格?

    服务网格(Service mesh)是当前新兴的架构模式,越来越受到人们的青睐.与Kubernetes一起,服务网格可以形成一个强大的平台,它可以解决在微服务集群或服务基础设施上发现的高度分布式环境中 ...

  5. HDFS 05 - HDFS 的元数据管理(FSImage、EditLog、Checkpoint)

    目录 1 - NameNode 的启动流程 2 - NameNode 的元数据 2.1 EditLog 操作日志 2.2 查看 EditLog 文件 2.3 FSImage 元数据镜像 2.4 查看 ...

  6. webdriver中的等待——主要讲解WebDriverWait()

    webdriver中的等待--主要讲解WebDriverWait() 强制等待:sleep() 隐式等待:implicitly_wait() 显示等待:WebDriverWait() 与until() ...

  7. Oracle数据库使用pfile启动还是spfile启动---oracle

    查看数据库使用pfile启动还是spfile启动 9i版本以后,一般是使用spfile启动,但前提是有这个spfile文件,如果同时存在spfile和pfile文件,会优先选择spfile模式启动数据 ...

  8. CAP 5.1 版本发布通告 - 你期待的 Redis 来了

    前言 今天,我们很高兴宣布 CAP 发布 5.1 版本正式版,在这个版本里我们同样引入了更多令人激动的新特性和改进,同时也得到越来越多人的喜爱. 得益于社区的反馈和贡献者的支持,在过去的两个月里,我们 ...

  9. Tensor基本理论

    Tensor基本理论 深度学习框架使用Tensor来表示数据,在神经网络中传递的数据均为Tensor. Tensor可以将其理解为多维数组,其可以具有任意多的维度,不同Tensor可以有不同的数据类型 ...

  10. VAE变分自编码器实现

    变分自编码器(VAE)组合了神经网络和贝叶斯推理这两种最好的方法,是最酷的神经网络,已经成为无监督学习的流行方法之一. 变分自编码器是一个扭曲的自编码器.同自编码器的传统编码器和解码器网络一起,具有附 ...