https://vjudge.net/problem/Gym-100783C

题意:

给出n个数,然后有m次查询,每次输入一个数x,问x能否由n个数中2个及2个以下的数相加组成。

思路:
题意很简单,但是如果直接去算要超时。

可以利用傅里叶,计算出两个卷积中的数相加的所有可能性。

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
using namespace std; #define LL long long
const double PI = acos(-1.0); // 复数结构体
struct Complex
{
double x, y; // 实部和虚部 x + yi
Complex(double _x = 0.0, double _y = 0.0)
{
x = _x;
y = _y;
}
Complex operator - (const Complex &b) const
{
return Complex(x - b.x, y - b.y);
}
Complex operator + (const Complex &b) const
{
return Complex(x + b.x, y + b.y);
}
Complex operator * (const Complex &b) const
{
return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
}
}; // 进行FFT和IFFT前的反转变换
// 位置i和(i二进制反转后的位置)互换
// len必须去2的幂
void change(Complex y[], int len)
{
int i, j, k;
for (i = , j = len / ; i < len - ; i++)
{
if (i < j)
{
swap(y[i], y[j]);
}
// 交换护卫小标反转的元素,i < j保证交换一次
// i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
k = len / ;
while (j >= k)
{
j -= k;
k /= ;
}
if (j < k)
{
j += k;
}
}
return ;
} // FFT
// len必须为2 ^ k形式
// on == 1时是DFT,on == -1时是IDFT
void fft(Complex y[], int len, int on)
{
change(y, len);
for (int h = ; h <= len; h <<= )
{
Complex wn(cos(-on * * PI / h), sin(-on * * PI / h));
for (int j = ; j < len; j += h)
{
Complex w(, );
for (int k = j; k < j + h / ; k++)
{
Complex u = y[k];
Complex t = w * y[k + h / ];
y[k] = u + t;
y[k + h / ] = u - t;
w = w * wn;
}
}
}
if (on == -)
{
for (int i = ; i < len; i++)
{
y[i].x /= len;
}
}
} const int maxn=+; Complex x1[*maxn];
int a[*maxn];
LL num[*maxn]; int main()
{
//freopen("D:\\input.txt","r",stdin);
int n,m;
while(~scanf("%d",&n))
{
memset(num,,sizeof(num));
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
a[n]=; n++; num[]++;
sort(a,a+n);
int len1=a[n-]+;
int len=;
while(len<*len1) len<<=;
for(int i=;i<len1;i++)
x1[i]=Complex(num[i],);
for(int i=len1;i<len;i++)
x1[i]=Complex(,);
fft(x1,len,);
for(int i=;i<len;i++)
x1[i]=x1[i]*x1[i];
fft(x1,len,-);
for(int i=;i<len;i++)
num[i]=(long long)(x1[i].x+0.5);
len=*a[n-];
//for(int i=0;i<n;i++)//减去2次选的同一个数
// num[a[i]+a[i]]--;
//for(int i=1;i<=len;i++) num[i]/=2;//选1 2和选2 1是一样的所以除2 //for(int i=0;i<=10;i++)
//printf("%d: %d\n",i,num[i]);
int ans=;
scanf("%d",&m);
while(m--)
{
int xx;
scanf("%d",&xx);
if(num[xx]) ans++;
}
printf("%d\n",ans);
}
}

Gym100783C Golf Bot(FFT)的更多相关文章

  1. LA6886 Golf Bot(FFT)

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

  2. 快速傅里叶(FFT)的快速深度思考

    关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...

  3. 【BZOJ3527】力(FFT)

    [BZOJ3527]力(FFT) 题面 Description 给出n个数qi,给出Fj的定义如下: \[Fj=\sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_{ ...

  4. 【BZOJ4827】【HNOI2017】礼物(FFT)

    [BZOJ4827][HNOI2017]礼物(FFT) 题面 Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一 个送给她.每 ...

  5. FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)

    前言 众所周知,这两个东西都是用来算多项式乘法的. 对于这种常人思维难以理解的东西,就少些理解,多背板子吧! 因此只总结一下思路和代码,什么概念和推式子就靠巨佬们吧 推荐自为风月马前卒巨佬的概念和定理 ...

  6. 【BZOJ4503】两个串(FFT)

    [BZOJ4503]两个串(FFT) 题面 给定串\(S\),以及带通配符的串\(T\),询问\(T\)在\(S\)中出现了几次.并且输出对应的位置. \(|S|,|T|<=10^5\),字符集 ...

  7. 【BZOJ4259】残缺的字符串(FFT)

    [BZOJ4259]残缺的字符串(FFT) 题面 给定两个字符串\(|S|,|T|\),两个字符串中都带有通配符. 回答\(T\)在\(S\)中出现的次数. \(|T|,|S|<=300000\ ...

  8. 【51Nod1258】序列求和V4(FFT)

    [51Nod1258]序列求和V4(FFT) 题面 51Nod 多组数据,求: \[Ans=\sum_{i=1}^ni^k,n\le 10^{18},k\le50000\] 题解 预处理伯努利数,时间 ...

  9. 【CF528D】Fuzzy Search(FFT)

    [CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...

随机推荐

  1. 排序算法review<2>--Shell 排序

    shell排序方法也是一种插入排序算法,于1959年由D.L.Shell提出,其基本方法是:首先将带排序文件分为d1(d1<n)组,将所有彼此之间间隔为d和d的倍数的记录放在一组中,然后在组内进 ...

  2. 病毒侵袭持续中---hdu3065(AC自动机模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 模板题,没什么好说的... #include<stdio.h> #include&l ...

  3. 源码包安装(Python mysql redis)

    一  源码包安装 (1)python3.6源码包安装 ./configure ------> 定制功能 make make install mysql 源码包 cmake make make i ...

  4. django-luffycity-购物车接口

    一  基本功能 -添加购物车 -详见代码 -修改课程价格策略 -put或者patch {"course_id": "1", "policy_id&qu ...

  5. onNewIntent 作用

    按 home 键,再打开app,会调用onNewIntent() 按 back 键,再打开app,会调用onCreate() public class MainActivity extends Act ...

  6. redis实现cache系统实践(六)

    1. 介绍 rails中就自带有cache功能,不过它默认是用文件来存储数据的.我们要改为使用redis来存储.而且我们也需要把sessions也存放到redis中.关于rails实现cache功能的 ...

  7. 关于理财和买房 http://shouce.jb51.net/phpcms/ https://www.bj.cmbchina.com/bjtransweb/wsgzd_employ/login.jsp

    对于绝大多数家境普通的年轻人来说,青年阶段无疑是一生中手头最紧的时候.原因很简单,这个阶段花钱最多,挣钱却最少.年轻人收入往往是硬性的低,开支却往往是硬性   的高.已经加班到晕头转向的小职员,很难再 ...

  8. w命令

    命 令:w 功能说明:显示目前登入系统的用户信息. 语 法:w [-fhlsuV][用户名称] 补充说明:执行这项指令可得知目前登入系统的用户有那些人,以及他们正在执行的程序.单独执行w 指令会显示所 ...

  9. matplotlib中的legend()—显示图例

    源自  matplotlib中的legend()——用于显示图例 -- 博客园 http://www.cnblogs.com/yinheyi/p/6792120.html legend()的一个用法: ...

  10. Get a better look at the 2014 Nike Hyperrev

    There's a couple of Nike Hyperrev For Sale Delay climax frames lead that will list for this calendar ...