2019 ICPC上海网络赛 C. Triple(暴力+FFT)

题面

在这里插入图片描述

题意
在三个集合中各选一条边,组成三角或两边之和等于第三条边的方案数

题解
类似题为HDU 4609 3-idiots,那道题是在一个集合中选三个边组成三角

首先用FFT确定a+b的方案数,然后让第三条边作为最大,用所有方案数减去不合法方案。
另外就是这题非常的恶心。小于1000的用暴力求卷积,FFT会T。

// #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <vector>
#include <assert.h>
#include <cmath>
#include <ctime>
using namespace std;
#define me(x,y) memset((x),(y),sizeof (x))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define SGN(x) ((x)>0?1:((x)<0?-1:0))
#define ABS(x) ((x)>0?(x):-(x))
// #define int __int128

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int maxn = 6e5+10;
const int inf = __INT32_MAX__;
const ll INF = __LONG_LONG_MAX__;
const int MOD = 1e9+7;
const double eps = 1e-8;
const double pi = std::acos(-1);
const string cars[] = {"?","?","?"};

struct Complex{
    double r,i;
    Complex(double _r = 0.0, double _i = 0.0){r = _r,i = _i;}
    Complex operator + (const Complex& a) const {
        return Complex(r+a.r,i+a.i);
    }
    Complex operator * (const Complex& a) const {
        return Complex(r*a.r-i*a.i,r*a.i+i*a.r);
    }
    Complex operator - (const Complex& a) const {
        return Complex(r-a.r,i-a.i);
    }
};
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]);
		k = len/2;
		while(j >= k){
			j -= k;
			k /= 2;
		}
		if(j < k)j += k;
	}
}
void fft(Complex y[],int len ,int fg){
    change(y,len);
    for(int h = 2; h <= len; h <<= 1){
        Complex wn(cos(-fg*2*pi/h),sin(-fg*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(fg == -1){
        for(int i = 0; i < len; ++i){
            y[i].r /= len;
        }
    }
}
ll A[maxn],B[maxn],C[maxn];
ll AA[maxn],BB[maxn],CC[maxn];
Complex x[maxn],y[maxn],z[maxn];
ll sum[maxn],tmp[maxn];
int mx,n;
ll solve1(ll *a,ll *b,ll *c){
	for(int i = 0; i <= c[n-1]; ++i) sum[i]  = 0;
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			sum[a[i]+b[j]]++;
		}
	}
	ll ans = 0;
	for(int i = 1; i <= c[n-1]; ++i) sum[i] += sum[i-1];
	for(int i = 0; i < n; ++i) ans += sum[c[i]-1];
	return ans;
}
ll solve2(ll *a,ll *b,ll *c){
	int len = 1;
	while(len <= (n << 1)) len<<=1;
	for(int i = 0; i <= mx; ++i) x[i] = Complex(a[i],0);
	for(int i = 0; i <= mx; ++i) y[i] = Complex(b[i],0);
	for(int i = mx+1; i <= len; ++i) x[i] = Complex(0,0),y[i] = Complex(0,0);
	fft(x,len,1),fft(y,len,1);
	for(int i = 0; i < len; ++i) z[i] = x[i]*y[i];
	fft(z,len,-1);
	ll ans = 0;
	for(int i = 0; i <= c[n-1]; ++i) sum[i] = (int)(z[i].r+0.5);
	for(int i = 1; i <= c[n-1]; ++i) sum[i] += sum[i-1];
	for(int i = 0; i < n; ++i) ans += sum[c[i]-1];
	return ans;
}
int main(){
    ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        freopen("1in.in","r",stdin);
        freopen("1out.out","w",stdout);
    #endif
    
    int t,ca = 1;cin>>t;
    while(t--){
        cin>>n;
		mx = 0;
        me(AA,0),me(BB,0),me(CC,0);
        for(int i = 0; i < n; ++i) cin>>A[i],AA[A[i]]++,mx=MAX(mx,A[i]);sort(A,A+n);
        for(int i = 0; i < n; ++i) cin>>B[i],BB[B[i]]++,mx=MAX(mx,B[i]);sort(B,B+n);
        for(int i = 0; i < n; ++i) cin>>C[i],CC[C[i]]++,mx=MAX(mx,C[i]);sort(C,C+n);
		if(n <= 1000){
			cout<<"Case #"<<(ca++)<<": "<<1ll*n*n*n-solve1(A,B,C)-solve1(A,C,B)-solve1(B,C,A)<<endl;
		}
		else {
			cout<<"Case #"<<(ca++)<<": "<<1ll*n*n*n-solve2(AA,BB,C)-solve2(AA,CC,B)-solve2(BB,CC,A)<<endl;
		}
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值