Golf Bot

题目连接:

http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129724

Description

Do you like golf? I hate it. I hate golf so much that I

decided to build the ultimate golf robot, a robot that

will never miss a shot. I simply place it over the ball,

choose the right direction and distance and, flawlessly,

it will strike the ball across the air and into the hole.

Golf will never be played again.

Unfortunately, it doesn’t work as planned. So, here

I am, standing in the green and preparing my first

strike when I realize that the distance-selector knob

built-in doesn’t have all the distance options! Not everything

is lost, as I have 2 shots.

Given my current robot, how many holes will I be

able to complete in 2 strokes or less? The ball must be

always on the right line between the tee and the hole.

It isn’t allowed to overstep it and come back.

Input

The input file contains several test cases, each of them

as described below.

The first line has one integer: N, the number of

different distances the Golf Bot can shoot. Each of

the following N lines has one integer, ki

, the distance

marked in position i of the knob.

Next line has one integer: M, the number of holes in this course. Each of the following M lines has

one integer, dj , the distance from Golf Bot to hole j.

Constraints:

1 ≤ N, M ≤ 200 000

1 ≤ ki

, dj ≤ 200 000

Output

For each test case, you should output a single integer, the number of holes Golf Bot will be able to

complete. Golf Bot cannot shoot over a hole on purpose and then shoot backwards.

Sample Output Explanation

Golf Bot can shoot 3 different distances (1, 3 and 5) and there are 6 holes in this course at distances

2, 4, 5, 7, 8 and 9. Golf Bot will be able to put the ball in 4 of these:

• The 1st hole, at distance 2, can be reached by striking two times a distance of 1.

• The 2nd hole, at distance 4, can be reached by striking with strength 3 and then strength 1 (or

vice-versa).

• The 3rd hole can be reached with just one stroke of strength 5.

• The 5th hole can be reached with two strikes of strengths 3 and 5.

Holes 4 and 6 can never be reached

Sample Input

3

1

3

5

6

2

4

5

7

8

9

Sample Output

4

Hint

题意

给你n个数,然后再给你一个数k,问这个数是否就是那n个数中的一个,或者说这个数可以由这n个数中的两个构成(可以是自己*2)

题解:

裸的不行的FFT,直接做就好了。

代码

#include<bits/stdc++.h>

using namespace std;

const int N = 1200040;
const double pi = acos(-1.0); int len; struct Complex
{
double r,i;
Complex(double r=0,double i=0):r(r),i(i) {};
Complex operator+(const Complex &rhs)
{
return Complex(r + rhs.r,i + rhs.i);
}
Complex operator-(const Complex &rhs)
{
return Complex(r - rhs.r,i - rhs.i);
}
Complex operator*(const Complex &rhs)
{
return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);
}
} va[N],vb[N]; void rader(Complex F[],int len) //len = 2^M,reverse F[i] with F[j] j为i二进制反转
{
int j = len >> 1;
for(int i = 1;i < len - 1;++i)
{
if(i < j) swap(F[i],F[j]); // reverse
int k = len>>1;
while(j>=k)
{
j -= k;
k >>= 1;
}
if(j < k) j += k;
}
} void FFT(Complex F[],int len,int t)
{
rader(F,len);
for(int h=2;h<=len;h<<=1)
{
Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h));
for(int j=0;j<len;j+=h)
{
Complex E(1,0); //旋转因子
for(int k=j;k<j+h/2;++k)
{
Complex u = F[k];
Complex v = E*F[k+h/2];
F[k] = u+v;
F[k+h/2] = u-v;
E=E*wn;
}
}
}
if(t==-1) //IDFT
for(int i=0;i<len;++i)
F[i].r/=len;
} void Conv(Complex a[],Complex b[],int len) //求卷积
{
FFT(a,len,1);
FFT(b,len,1);
for(int i=0;i<len;++i) a[i] = a[i]*b[i];
FFT(a,len,-1);
}
int n;
int a[N];
long long num[N],sum[N];
void solve()
{
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
memset(va,0,sizeof(va));
memset(vb,0,sizeof(vb));
int Mx = 0;
for(int i=0;i<n;i++)
{
int x;scanf("%d",&a[i]);
Mx = max(Mx,a[i]);
num[a[i]]=1;
}
Mx*=2;
len=1;
while(len<=Mx+1)len*=2;
sort(a,a+n);
for(int i=0;i<=len;i++)
{
va[i].r=num[i];
va[i].i=0;
vb[i].r=va[i].r;
vb[i].i=0;
}
Conv(va,vb,len);
for(int i=0;i<len;i++)
num[i]+=(long long)(va[i].r+0.5);
int cnt = 0;
int q;scanf("%d",&q);
while(q--){
int bbb;
scanf("%d",&bbb);
if(num[bbb])cnt++;
}
printf("%d\n",cnt);
}
int main()
{
while(scanf("%d",&n)!=EOF)solve();
return 0;
}

UVALive 6886 Golf Bot FFT的更多相关文章

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

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

  2. UVALIVE6886 Golf Bot (FFT)

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

  3. Gym 100783C Golf Bot FFT

    大致题意: 给你N个整数和M个整数,问这M个数中,有几个数可以表达成那N个整数中一个或者两个整数的和. 分析: 算是半个裸的FFT.FFT可以用来在nlongn时间内求高精度乘法,我们先模拟一下乘法. ...

  4. LA6886 Golf Bot(FFT)

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

  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. UVALive - 4671 K-neighbor substrings (FFT+哈希)

    题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串A和模式串B,求A中有多少与B海明距离<=k的不同子串 分析:将字符a视作1,b视作0.则A与B中都是a的位置乘积是1.现将B ...

  9. FFT题集

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

随机推荐

  1. Tomcat安装与优化

    Tomcat安装与优化 1.安装jdk环境 最新的JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ...

  2. MAVLink v1.0详解——结构

    本文针对 MAVLink v1.0版本,协议版本:3. MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议.是无人飞行器和地面站(Ground C ...

  3. C# 托管资源 与 非托管资源

    C# 托管资源 与 非托管资源 托管资源一般是指被CLR控制的内存资源,这些资源的管理可以由CLR来控制,.NET可以自动进行回收,主要是指托管堆上分配的内存资源.例如程序中分配的对象,作用域内的变量 ...

  4. 使用gradle编译安卓APK

    一.安装JDK 在安装Gradle之前需要先安装JDK,由于安装的是Gradle是4.4所以需要安装JDK1.8. 之前编译总是提示如下错误就是由于先安装的jdk1.7然后安装的1.8造成的,在Gra ...

  5. java 多线程总结篇1之——基本概念

    1.什么是线程 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程.(进程是资源分配的最小单位) 线程:同一类线程共享代码和数据空间,每个线程 ...

  6. SQLSERVER中的非工作时间不得插入数据的触发器的实现

    create trigger trigger_nameon table_namefor insert,update,deleteasif (datepart(yy,getdate())%4=0 or ...

  7. JDBC连接池和DBUtils

    本节内容: JDBC连接池 DBUtils 一.JDBC连接池 实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程,为了解决此类性能问题,通常情况我们采取连接池技术,来共享连接Conne ...

  8. Kafka(一)Kafka的简介与架构

    一.简介 1.1 概述 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/ng ...

  9. Java语法知识总结

    一:java概述: 1991 年Sun公司的James Gosling等人开始开发名称为 Oak 的语言,希望用于控制嵌入在有线电视交换盒.PDA等的微处理器: 1994年将Oak语言更名为Java: ...

  10. Sample ASP.NET IHttpHandler

    LoggerHandler.cs using System; using System.Collections.Generic; using System.Diagnostics; using Sys ...