2016 acm 香港网赛A A+B Problem

Given N integers in the range [−50000,50000], how many ways are there to pick three integers ai, aj, ak, such that i, j, k are pairwise distinct and ai+aj=ak? Two ways are different if their ordered triple (i,j,k) of indices are different.

Input

The first line of input consists of a single integer N (1≤N≤200000). The next line consists of N space-separated integers a1,a2,…,aN.

Output

Output an integer representing the number of ways.

Sample Input 1
4
1 2 3 4
Sample Output 1
4
Sample Input 2
6
1 1 3 3 4 6
Sample Output 2
10

题意

  从输入数据中找这样的组合 ( i , j , k ) , 使 ai + aj = ak 。输出组数。

题解

num[k]表示(ai,aj)= k的个数。然后将ai = aj的那种重复的去掉。

然后计算当a[i] = k时的(i,j,k)有多少种,很明显就是 num[ a[i]]种,

注意有一种情况是有0的时候,因为有可能会自己加了0也等于ai 。所以要统计一下0有多少个,减了0就可以了,注意(i,j)也是有顺序要求的,(i,j)和(j,i)是2个不同的,所以还要处理有0的时候要乘以2倍。
然后是因为有负数,我们要全部都加上50000变成正数就好了。

(HDU4609的变形)

思路

  1. 输入数据,保存在数组a中,输入时记录0的个数(zero)。
  2. 将每个数字出现的个数保存在num数组中,因为有负数,保存的时候+50000。
  3. 将数组a排序,a的最大值+50000就是num数组卷积之前的长度。
  4. 卷积num数组时,长度要增长为一个大于卷积之前长度*2-1的2的整数次幂。
  5. 由于涉及到复数,将num数组转化为复数数组F,num数组输入到a的最大值处(len1),增长的长度(len1到len)置为0.
  6. 计算卷积:先用FFT法求加长序列的DFT频谱(执行fft函数后的F数组),再将此时的F数组各项平方,再用IFFT求DFT频谱乘积的逆变换,便得两序列的离散线卷积。
  7. 将计算卷积之后的F数组的实部返回到num数组中。此时num[k+100000]的值为i+j=k的组数。公式是这样的 i,j<ni+j=kaibj,k=0,1,...,2n1
  8. 由于i和j不能相同,把数组a中所有元素*2对应位置的num数组卷积-1.
  9. 用数组a中的元素搜索num数组,将num数组的元素累加,并减去0个数的两倍。如果a中的元素本身是0,减去zero-1的两倍。
  10. 输出结果。

AC代码


#include <iostream>
#include <cmath>   
#include <algorithm>   
using namespace std;  

const int N = 2e5+10;  
const double pi = acos(-1.0);  


struct Complex{  
    double r,i;  
    Complex(double r=0,double i=0):r(r),i(i){
        //声明复数:Complex XXX(XX,XX);
    };  
    //定义复数的加减乘运算
    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);  
    } 

};

int len;
long long a[4*N];    
Complex F[4*N];    
long long num[4*N];  //每个数字出现的个数
int n;  
long long zero = 0;   //输入值中0的个数


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;  
    }  
} 
//FFT  
void FFT(Complex F[],int len,int t){   
    for(int h = 2;h <= len; h <<= 1){ //分治后计算长度为h的DFT   
        Complex wn(cos(-t * 2 * pi / h) , sin( -t * 2 * pi / h)); //单位复根e^(2*PI/m)用欧拉公式展开  
        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 F[],int len){  
    //用FFT法求加长序列的DFT频谱
    FFT(F,len,1);  
    //计算DFT频谱的平方
    for(long long i = 0; i < len; ++i){
        F[i] = F[i] * F[i];  
    }
    //用IFFT求DFT频谱乘积的逆变换,便得两序列的离散线卷积
    FFT(F,len,-1);  
}  

int main(int argc, char *argv[]){  
    //初始化 输入数据
    cin >> n;
    memset(num,0,sizeof(num));             
    for(int i = 0; i < n; i++){    
        cin >> a[i];  
        if(a[i] == 0){
            zero++; 
        } 
        num[a[i] + 50000]++;    
    }      
    sort(a, a + n);    
    int len1 = a[n-1] + 50000 + 1; // 输入最大值+50001 
    len = 1; 
    //计算卷积后F数组长度 使len为2的整数次幂 并且 len >= 2 * len1 - 1  
    while(len < len1 * 2){
        len <<= 1;  
    }
    //num数组变为复数形式
    for(int i = 0; i < len1; i++){    
        F[i] = Complex(num[i],0);    
    }
    //初始化F数组
    for(int i = len1; i < len; i++){   
        F[i] = Complex(0,0);    
    } 
    //计算卷积 
    Conv(F,len);  
    //num数组现在是卷积后的结果,num[k+100000]的值为i+j=k的组数    
    for(int i = 0; i < len; i++){    
        num[i] = (long long)(F[i].r + 0.5); //四舍五入   
    }     
    //本身和本身组合是不行的,减掉取两个相同的组合  
    for(int i = 0; i < n; i++){    
        num[a[i] + a[i] + 2 * 50000]--;   
    }
    long long cnt = 0; //组数
    //找到输入在num数组中对应的位置 计算cnt 对0进行特殊处理 
    for(int i = 0; i < n; i++){    
       if(a[i] != 0){  
         cnt += num[a[i] + 2 * 50000];  
         cnt -= zero * 2;  
       }else{  
            cnt += num[a[i] + 2 * 50000];  
            cnt -= (zero - 1) * 2;  
       }  
    }    
    cout << cnt << endl;   
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值