您的位置:首页 > 编程语言 > Go语言

UVALive 6886 Golf Bot (FFT)

2017-08-07 10:44 549 查看
题目链接 : 点击打开链接

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个球洞,每个数代表m个球洞的距离。

问你可以击球两次,有多少个洞可以进。

就是让你求m个数有多少个能由这n个数中的两个相加得到(可以自己加自己,也可以只有本身)。

我们可以把在第一个数组中出现的数当成是以这个数为次数,系数为1的二项式中的一项,再把次数为0的项的系数也设为1(因为可以只击球一次),然后这个数组和自己做一次卷积,就能求出所有数的组合了。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#define LL long long
using namespace std;

const double PI = acos(-1.0);
//复数结构体
struct complex
{
double r,i;
complex(double _r = 0.0,double _i = 0.0)
{
r = _r; i = _i;
}
complex operator +(const complex &b)
{
return complex(r+b.r,i+b.i);
}
complex operator -(const complex &b)
{
return complex(r-b.r,i-b.i);
}
complex operator *(const complex &b)
{
return complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
/*
* 进行FFT和IFFT前的反转变换。
* 位置i和 (i二进制反转后位置)互换
* len必须去2的幂
*/
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]);
//交换互为小标反转的元素,i<j保证交换一次
//i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
k = len/2;
while(j >= k)
{
j -= k;
k /= 2;
}
if(j < k) j += k;
}
}
/*
* 做FFT
* len必须为2^k形式,
* on==1时是DFT,on==-1时是IDFT
*/
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].r /= len;
}
const int MAXN = 1200040;
complex x[MAXN];
LL sum[MAXN];
int dis[MAXN/2];

int main(void)
{
int n,m,i,j;
while(scanf("%d",&n)==1)
{
memset(dis,0,sizeof(dis));
int mxdis = 0;
for(i=1;i<=n;i++)
{
int t;
scanf("%d",&t);
dis[t] = 1;
mxdis = max(mxdis,t);
}
int len = 1;

while(len <= mxdis*2)
len <<= 1;
x[0] = complex(1,0);
for(i=1;i<len;i++)
{
if(dis[i] == 1)
x[i] = complex(1,0);
else
x[i] = complex(0,0);
}
fft(x,len,1);
for(i=0;i<len;i++)
x[i] = x[i]*x[i];
fft(x,len,-1);
memset(sum,0,sizeof(sum));
for(i=0;i<len;i++)
sum[i] = (LL)(x[i].r + 0.5);
scanf("%d",&m);
int cnt = 0;
for(i=1;i<=m;i++)
{
int t;
scanf("%d",&t);
if(sum[t] > 0)
cnt++;
}
printf("%d\n",cnt);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: