UVALive 6886 Golf Bot FFT
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的更多相关文章
- UVALive - 6886 Golf Bot 多项式乘法(FFT)
题目链接: http://acm.hust.edu.cn/vjudge/problem/129724 Golf Bot Time Limit: 15000MS 题意 给你n个数,m个查询,对于每个查询 ...
- UVALIVE6886 Golf Bot (FFT)
题意:打高尔夫 给你n个距离表示你一次可以把球打远的距离 然后对于m个询问 问能否在两杆内把球打进洞 题解:平方一下就好 注意一下x0的系数为1表示打一杆 才发现数组应该开MAXN * 4 之前写的题 ...
- Gym 100783C Golf Bot FFT
大致题意: 给你N个整数和M个整数,问这M个数中,有几个数可以表达成那N个整数中一个或者两个整数的和. 分析: 算是半个裸的FFT.FFT可以用来在nlongn时间内求高精度乘法,我们先模拟一下乘法. ...
- LA6886 Golf Bot(FFT)
题目 Source https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page= ...
- HNU11376:Golf Bot
Problem description Input The first line has one integer: N, the number of different distances the G ...
- Gym100783C Golf Bot(FFT)
https://vjudge.net/problem/Gym-100783C 题意: 给出n个数,然后有m次查询,每次输入一个数x,问x能否由n个数中2个及2个以下的数相加组成. 思路:题意很简单,但 ...
- [Swerc2014 C]Golf Bot
题意:给你N个数字,每次利用这N个数字中最多两个数字进行加法运算,来得到目标中的M个数字. Solution: 我们先来看看多项式乘法:\(A(x)=\sum_{i=0}^{n-1}a_ix^i\), ...
- UVALive - 4671 K-neighbor substrings (FFT+哈希)
题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串A和模式串B,求A中有多少与B海明距离<=k的不同子串 分析:将字符a视作1,b视作0.则A与B中都是a的位置乘积是1.现将B ...
- FFT题集
FFT学习参考这两篇博客,很详细,结合这看,互补. 博客一 博客二 很大一部分题目需要构造多项式相乘来进行计数问题. 1. HDU 1402 A * B Problem Plus 把A和B分别当作多项 ...
随机推荐
- Linux信息搜集
## 1.取证工具 - LiME 内存获取工具 - volatility 内存分析工具 ## 2.机器信息收集 #sysinfo 16 # # 查看当前登录用户 who > who.txt # ...
- aarch64_a1
AGReader-1.2-16.fc26.aarch64.rpm 2017-02-14 07:01 50K fedora Mirroring Project ATpy-0.9.7-11.fc26.no ...
- iptables NAT规则【转】
nat表需要的三个链: 1.PREROUTING:可以在这里定义进行目的NAT的规则,因为路由器进行路由时只检查数据包的目的ip地址,所以为了使数据包得以正确路由,我们必须在路由之前就进行目的NAT; ...
- 007_苹果Mac系统锁屏不待机效果设置方法介绍
Mac如何设置锁屏不断网?Mac如何设置锁屏不待机?这是一个非常麻烦的设置,有时候一锁屏幕电脑就跟着待机了,这非常的麻烦,所以今天小编就用图文教程的方式教大家Mac如何设置锁屏不断网Mac如何设置锁屏 ...
- GO里的“指针”
指针 *T即为类型T的指针 &t即为获取变量t的地址 *p即为获取指针变量所指向的内容 var p *int 指针的*在左边 类型在右边.这里的 *int就是一个指针类型. 跟int str ...
- 数据库-mysql视图
视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用 一:创建视图 create view view ...
- TDictionary 与 TObjectDictionary
TDictionary 与 TObjectDictionary 的区别是 : TObjectDictionary 可以做到 free的时候 里面的对象 一并free,从而不会出现内存 泄露. 用途: ...
- Jackson jsonToBean 忽略 Bean中不存在属性的匹配。
1. jsonToBean时,json中有的字段,bean中没有 无法匹配时,忽略此字段,不抛出异常(默认是抛出异常的) 方案一(局部):在class上加 @JsonIgnoreProperties( ...
- sql server 2000系统表sysproperties在SQL 2008中无效的问题
Sqlserver有一个扩展属性系统表sysproperties,因为只接触过MSSQL2005及以后的版本,在生产库2008版本及联机文档上搜了下都找不到这个系统表,后来发现这个系统表在2005版本 ...
- Executor
一.为什么需要Executor?为了更好的控制多线程,JDK提供了一套线程框架Executor,帮助开发人员有效的进行线程控制.他们都在java.util.concurrent包中,是JDK并发包的核 ...