LA6886 Golf Bot(FFT)
题目
Source
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
分析
题目大概说打高尔夫球,没打一次球球能滚的距离有n种情况,现在已知m个洞的位置,球只能往前打,最多能打两下,问有多少个洞能打进球。
构造两个多项式,指数表示距离,系数表示存不存在(1或0),然后用FFT相乘,结果各个系数就表示打2次打到对应指数距离的方案数,算是母函数的东西吧。。很简单的题。
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 555555
const double PI=acos(-1.0); struct Complex{
double real,imag;
Complex(double _real,double _imag):real(_real),imag(_imag){}
Complex(){}
Complex operator+(const Complex &cp) const{
return Complex(real+cp.real,imag+cp.imag);
}
Complex operator-(const Complex &cp) const{
return Complex(real-cp.real,imag-cp.imag);
}
Complex operator*(const Complex &cp) const{
return Complex(real*cp.real-imag*cp.imag,real*cp.imag+cp.real*imag);
}
void setValue(double _real=0,double _imag=0){
real=_real; imag=_imag;
}
}; int len;
Complex wn[MAXN],wn_anti[MAXN]; void FFT(Complex y[],int op){
for(int i=1,j=len>>1,k; i<len-1; ++i){
if(i<j) swap(y[i],y[j]);
k=len>>1;
while(j>=k){
j-=k;
k>>=1;
}
if(j<k) j+=k;
}
for(int h=2; h<=len; h<<=1){
Complex Wn=(op==1?wn[h]:wn_anti[h]);
for(int i=0; i<len; i+=h){
Complex W(1,0);
for(int j=i; j<i+(h>>1); ++j){
Complex u=y[j],t=W*y[j+(h>>1)];
y[j]=u+t;
y[j+(h>>1)]=u-t;
W=W*Wn;
}
}
}
if(op==-1){
for(int i=0; i<len; ++i) y[i].real/=len;
}
}
void Convolution(Complex A[],Complex B[],int n){
for(len=1; len<(n<<1); len<<=1);
for(int i=n; i<len; ++i){
A[i].setValue();
B[i].setValue();
} FFT(A,1); FFT(B,1);
for(int i=0; i<len; ++i){
A[i]=A[i]*B[i];
}
FFT(A,-1);
} bool vis[MAXN];
Complex A[MAXN],B[MAXN]; int main(){
for(int i=0; i<MAXN; ++i){
wn[i].setValue(cos(2.0*PI/i),sin(2.0*PI/i));
wn_anti[i].setValue(wn[i].real,-wn[i].imag);
}
int n,m,a;
while(scanf("%d",&n)==1){
memset(vis,0,sizeof(vis));
int mx=0;
for(int i=0; i<n; ++i){
scanf("%d",&a);
A[a].setValue(1);
B[a].setValue(1);
vis[a]=1;
mx=max(mx,a);
}
Convolution(A,B,mx+1);
for(int i=0; i<len; ++i){
int tmp=(int)(A[i].real+0.5);
if(tmp) vis[i]=1;
A[i].setValue(); B[i].setValue();
}
int ans=0;
scanf("%d",&m);
for(int i=0; i<m; ++i){
scanf("%d",&a);
if(vis[a]) ++ans;
}
printf("%d\n",ans);
}
return 0;
}
LA6886 Golf Bot(FFT)的更多相关文章
- Gym100783C Golf Bot(FFT)
https://vjudge.net/problem/Gym-100783C 题意: 给出n个数,然后有m次查询,每次输入一个数x,问x能否由n个数中2个及2个以下的数相加组成. 思路:题意很简单,但 ...
- 快速傅里叶(FFT)的快速深度思考
关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...
- 【BZOJ3527】力(FFT)
[BZOJ3527]力(FFT) 题面 Description 给出n个数qi,给出Fj的定义如下: \[Fj=\sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_{ ...
- 【BZOJ4827】【HNOI2017】礼物(FFT)
[BZOJ4827][HNOI2017]礼物(FFT) 题面 Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一 个送给她.每 ...
- FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)
前言 众所周知,这两个东西都是用来算多项式乘法的. 对于这种常人思维难以理解的东西,就少些理解,多背板子吧! 因此只总结一下思路和代码,什么概念和推式子就靠巨佬们吧 推荐自为风月马前卒巨佬的概念和定理 ...
- 【BZOJ4503】两个串(FFT)
[BZOJ4503]两个串(FFT) 题面 给定串\(S\),以及带通配符的串\(T\),询问\(T\)在\(S\)中出现了几次.并且输出对应的位置. \(|S|,|T|<=10^5\),字符集 ...
- 【BZOJ4259】残缺的字符串(FFT)
[BZOJ4259]残缺的字符串(FFT) 题面 给定两个字符串\(|S|,|T|\),两个字符串中都带有通配符. 回答\(T\)在\(S\)中出现的次数. \(|T|,|S|<=300000\ ...
- 【51Nod1258】序列求和V4(FFT)
[51Nod1258]序列求和V4(FFT) 题面 51Nod 多组数据,求: \[Ans=\sum_{i=1}^ni^k,n\le 10^{18},k\le50000\] 题解 预处理伯努利数,时间 ...
- 【CF528D】Fuzzy Search(FFT)
[CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...
随机推荐
- September 17th 2016 Week 38th Saturday
Our eyes do not show a lack of beauty, but a lack of observation. 世上不是缺少美,而是缺少发现美的眼睛. Don't pay too ...
- mongodb启动后台服务
将MongoDB部署在服务器机子上时mongodb的实例应为后台服务进行的方式运行,而非前台进程,否则远程会话一关闭mongodb也跟着关闭了.本文介绍mongodb后台服务进程开启和关闭的操作. 开 ...
- Linux Shell多命令执行
有三种: :只是顺序执行,命令之间没有任何关联,不相互影响.如 ls;date;cd /etc/ 如,创建100M的文件. && 命令之间有关系,只有前一条命令正确执行才会执行下面一 ...
- NYOJ题目96 n-1位数
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAscAAAJgCAIAAADpjVkvAAAgAElEQVR4nO3du04jS/gv7H0T5FwIsa ...
- NYOJ题目817英文藏头诗
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtEAAAKXCAIAAADLqoEhAAAgAElEQVR4nO3dO1LrzNbG8W8S5AyE2D
- EMS-Demo 雇员管理系统演示
做了一个小小的雇员管理系统,主要使用了JTable,然后比较得意的地方是实现了拼音搜索,感觉很高大上其实只要引入一个Jpinyin.jar就可以了(网上到处都有下载或者去我的git项目的lib中下载) ...
- 数据结构和算法 – 12.高级查找算法(下)
哈希(散列)技术既是一种存储方法,也是一种查找方法.然而它与线性表.树.图等结构不同的是,前面几种结构,数据元素之间都存在某种逻辑关系,可以用连线图示表示出来,而哈希技术的记录之间不存在什么逻辑关系, ...
- Java集合源码学习(三)LinkedList分析
前面学习了ArrayList的源码,数组是顺序存储结构,存储区间是连续的,占用内存严重,故空间复杂度很大.但数组的二分查找时间复杂度小,为O(1),数组的特点是寻址容易,插入和删除困难.今天学习另外的 ...
- Ubuntu16.04 + Win 10 双系统 时间同步,启动项顺序,NumLock指示灯常亮
1. Ubuntu & win10 双系统时间同步: 先在ubuntu下更新一下时间,确保时间无误: sudo apt-get install ntpdate sudo ntpdate tim ...
- WCF 回调中操作线程
回调的类 [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = fals ...