题目链接:https://cn.vjudge.net/contest/280041#problem/B

题目大意:给你n个数,然后让你找满足a[i] + a[j] = a[k] 的情况总数。

具体思路:首先把每一种情况的个数算出来(两个数相加的结果),然后再就是去重的过程。

(因为题目中会有负数,我们可以全部转换成非负数去进行计算)

1,自己和自己相加。

2,1+0=1,0+1=1这个时候,1是使用了两次,所以需要去掉这种情况,就是去掉(0的总数)*2.

3,0+0=0,0+0=0,这个时候我们可以按照第一种的思路来(这个时候i!=j,因为自己加自己情况已经去掉了),把其中一个0看成(非0的数),然后再按照公式进行计算,不过计算的时候是(0的总数-1)*2.

AC代码:

 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<stdio.h>
using namespace std;
# define ll long long
const double PI = acos(-1.0);
const int maxn = 2e5+;
struct complex
{
double r,i;
complex(double _r = ,double _i = )
{
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);
}
};
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]);
k = len/;
while( j >= k)
{
j -= k;
k /= ;
}
if(j < k)
j += k;
}
}
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].r /= len;
}
const int T=5e4;
complex x1[maxn<<];
ll num[maxn<<],a[maxn<<],b[maxn<<];
int main()
{
int n;
scanf("%d",&n);
int ans=;
int len=;
while(len<maxn)
len<<=;
for(int i=; i<n; i++)
{
scanf("%lld",&a[i]);
if(a[i]==)
ans++;
b[a[i]+T]++;
}
for(int i=; i<len; i++)
{
x1[i]=complex(b[i],);
}
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]=(ll)(x1[i].r+0.5);
}
for(int i=; i<n; i++)
{
num[(a[i]+T)*]--;
}//重复的去掉
ll sum=;
// cout<<num[T+T]<<endl;
for(int i=; i<n; i++)
{
sum+=num[a[i]+*T];//比如说 1 2 3 ,我们现在要计算能组成3的个数,也就是3加上 2 个T,因为他的两个因子分别有一个T
sum-=(ans-(a[i]==))*;// 0+4 =4 ,4+0=4,这个时候4是用了两遍的,所以减去的就应该是ans*2。
// 对于0+0等于0,这种情况,如果说当前只有两个0的话,num[0]是等于2的(去重之后),这个时候我们就把其中一个0看成非0的,然后再按照上面的步骤进行计算。
}
printf("%lld\n",sum);
return ;
}

(FFT)A+B Problem的更多相关文章

  1. hihocoder 1388 fft循环矩阵

    #1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...

  2. hdu 5830 FFT + cdq分治

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. hdu----(1402)A * B Problem Plus(FFT模板)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. 【HDU1402】【FFT】A * B Problem Plus

    Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...

  5. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  6. A * B Problem Plus(fft)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 hdu_1402:A * B Problem Plus Time Limit: 2000/100 ...

  7. [Luogu 1919]【模板】A*B Problem升级版(FFT快速傅里叶)

    Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出 ...

  8. 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)

    洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...

  9. CF1153F Serval and Bonus Problem FFT

    CF1153F Serval and Bonus Problem 官方的解法是\(O(n ^ 2)\)的,这里给出一个\(O(n \log n)\)的做法. 首先对于长度为\(l\)的线段,显然它的答 ...

随机推荐

  1. redis启动停止+密码认证

    redis启动停止命令 ./bin/redis-server redis.conf ./bin/redis-cli -h 127.0.0.1 -p 6379 shutdown flushall ——& ...

  2. rethinking virtual network embedding..substrate support for path splitting and migration阅读笔记

    1.引言 网络虚拟化, 1.支持同一个底层网络有多种网络架构,每种架构定制一个应用或用户社区. 2.也可以让多个服务提供者在共同的物理基础设施上定制端到端的服务.如Voice over IP(VoIP ...

  3. Daily Scrum 10.22

    (写于10.22周四0晨) 昨天任务还未完成的继续完成任务. 每个人都查看自己的TFS,修改已经完成的任务状态,改为已关闭-已完成. 由于android studio运行过于慢,我们统一采取eclip ...

  4. [转帖]Linux系统/dev/mapper目录浅谈

    Linux系统/dev/mapper目录浅谈   Linux系统的一般的文件系统名称类似于/dev/sda1或/dev/hda1,但是今天在进行系统维护的时候,利用df -h 命令敲出了/dev/ma ...

  5. XShell中文乱码问题解决

    现象:XShell终端中输入中文显示乱码 原因:XShell终端的编码格式与服务器不同 解决:修改XShell终端的编码格式:菜单中点击,文件->属性->终端->编码,选择“UTF- ...

  6. Spring注解开发简要步骤

    1.除spring基本包外还需要下载AOP包 spring-aop-4.2.4.RELEASE.jar 2.导入约束(最后两行) <beans xmlns="http://www.sp ...

  7. springMVC返回给页面参数的三种形式

  8. BZOJ4066 简单题(KD-Tree)

    板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  9. Mouse Hunt CodeForces - 1027D(思维 找环)

    Medicine faculty of Berland State University has just finished their admission campaign. As usual, a ...

  10. js判断checkbox是否选中

    $('.div0 .checkbox1').prop('checked')选中返回 true未选中返回 false $('.div0').prop("checked", true) ...