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. 【Python项目】爬取新浪微博签到页

    基于微博签到页的微博爬虫 项目链接:https://github.com/RealIvyWong/WeiboCrawler/tree/master/WeiboLocationCrawler 1 实现功 ...

  2. openjudge-NOI 2.5基本算法之搜索 专题题解目录

    1.1700 八皇后问题 2.1756 八皇后 3.1789 算24

  3. 分布式git

    分布式 Git 你现在拥有了一个远程 Git 版本库,能为所有开发者共享代码提供服务,在一个本地工作流程下,你也已经熟悉 了基本 Git 命令.你现在可以学习如何利用 Git 提供的一些分布式工作流程 ...

  4. 一种获取xml文件某个节点内容的shell方法

    配置文件 config.xml <xml> <server> <name>srv-01</name> </server> <serve ...

  5. javascript本地缓存方案-- 存储对象和设置过期时间

    cz-storage 解决问题 1. 前端js使用localStorage的时候只能存字符串,不能存储对象 cz-storage 可以存储 object undefined number string ...

  6. Flask form

    用户登录 #!/usr/bin/env python # -*- coding:utf- -*- from flask import Flask, render_template, request, ...

  7. AdvStringGrid 单元格字体颜色、背景颜色

    procedure TForm5.Button1Click(Sender: TObject); var I: Integer; begin AdvStringGrid1.RowCount := ;// ...

  8. hihoCoder #1190 : 连通性·四(点的双连通分量模板)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了点问题,当服 ...

  9. Luogu P4894 【GodFly求解法向量】

    个人感觉我的解法比官方题解好理解得多 因为是任意一个法向量嘛,不妨设$x=1$ 然后解一个二元一次方程就可以解决了 但是因为要求输出三个整数 代码 #include<iostream> # ...

  10. Fix Valgrind's must-be-redirected error in Gentoo

    Last week, I tried to use Valgrind to identify potential memory related bugs, since segmentation fau ...