0
点赞
收藏
分享

微信扫一扫

计蒜客 ICPC2019 上海网络赛 C Triple(FFT + 组合计数)


计蒜客 ICPC2019 上海网络赛 C Triple(FFT + 组合计数)_组合计数

计蒜客 ICPC2019 上海网络赛 C Triple(FFT + 组合计数)_FFT_02

 

 

大致题意:给你三组边长,让你从三组边长里面各选出一条边,问组成非严格三角形的方案有多少种。这里非严格是指,可以退化为一条直线。

这题其实算是半个原题,详见HDU 4609。

那道题是一组边选三条构成严格三角形,这里是三组边。其实本质上类似。

参照那道题目的做法,我们枚举一条最大边,于是我们就可以知道另外两条边的和就是这条边的长度到最大长度这个区间内。我们要做的就是看,从另外两组边里面选择两天边,长度之和在那个区间范围内的有多少种组合。当然了这个组合得减去那些最大边不为枚举的边的情况。

这个组合数,还是一样考虑用FFT做。然后考虑去掉不合法的方案。之前分为了三种,但是发现那样子比较复杂,这里重新进行了归类。不合法可以分为两类:两条边其中一条边大于等于枚举边,另一条边小于枚举边的情况,此时方案数为满足上述条件组内二者边数量的乘积;另一种是两条边都大于枚举边的情况,方案数为边数的乘积。如此我们可以算出枚举边为最长边时候的所有方案。然后就是算枚举边与另外某一条边长度相等的等腰三角形的情况,方案为相等的边的条数,乘以剩下那一组里面范围在1到枚举边长度减一的条数。最后再把等边三角形的情况加上去就是最后的答案。

然后这题,卡常卡的很厉害,就是说如果是小数据的话,你不能用FFT求组合的方案数。对于n小于1000的情况,直接N方暴力枚举求出方案数,然后正常计算即可。具体见代码:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3fll
#define PI 3.1415926535
#define eps 1e-6
#define LL long long
#define pb push_back
#define fi first
#define se second
#define cl clear
#define si size
#define lb lower_bound
#define ub upper_bound
#define lowbit(x) x&-x
#define bug(x) cerr<<#x<<" : "<<x<<endl
#define mem(x) memset(x,0,sizeof x)
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;

const int N = 140007;
const int mod = 1e9 + 7;

namespace fft
{
struct Complex
{
double r,i;
Complex(double real=0.0,double image=0.0)
{
r=real; i=image;
}

inline Complex operator +(const Complex o){return Complex(r+o.r,i+o.i);}
inline Complex operator -(const Complex o){return Complex(r-o.r,i-o.i);}
inline Complex operator *(const Complex o){return Complex(r*o.r-i*o.i,r*o.i+i*o.r);}
} x[N<<1],y[N<<1],z[N<<1],tmp[N<<1];

inline void brc(Complex *y, int l)
{
register int i,j,k;
for( i = 1, j = l / 2; i < l - 1; i++)
{
if (i < j) swap(y[i], y[j]);
k = l / 2; while ( j >= k) j -= k,k /= 2;
if (j < k) j += k;
}
}

inline void FFT(Complex *y, int len, double on)
{
register int h, i, j, k;
Complex u, t; brc(y, len);
for(h = 2; h <= len; h <<= 1)
{
Complex wn(cos(on * 2 * PI / h), sin(on * 2 * PI / h));
for(j = 0; j < len; j += h)
{
Complex w(1, 0);
for(k = j; k < j + h / 2; k++)
{
u = y[k]; t = w * y[k + h / 2];
y[k] = u + t; y[k + h / 2] = u - t;
w = w * wn;
}
}
}
if (on<0) for (int i = 0; i < len; i++) y[i].r/=len;
}
}

int A[N<<1],B[N<<1],C[N<<1],l,n;
LL sum[N<<1],ans;
using namespace fft;

inline void solve(int *A,int *B,int *C,Complex *x,Complex *y)
{
if (n<=1000)
{
memset(sum,0,sizeof(sum[0])*l);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
sum[A[i]+B[j]]++;
for(int i=1;i<l;i++) sum[i]+=sum[i-1];
} else
{
sum[0]=0;
for(int i=0;i<l;i++)
tmp[i]=x[i]*y[i];
FFT(tmp,l,-1);
for(int i=1;i<l;i++)
sum[i]=(LL)(tmp[i].r+0.5)+sum[i-1];
}
for(int i=1;i<=n;i++)
{
ans+=sum[l-1]-sum[C[i]-1];
LL l1=lb(A+1,A+1+n,C[i])-A;
int l2=lb(B+1,B+1+n,C[i])-B;
ans-=(l1-1)*(n-l2+1)+(l2-1)*(n-l1+1);
ans-=(n-l2+1)*(n-l1+1);
int ll=lb(B+1,B+1+n,A[i])-B;
if (A[i]!=B[ll]) continue;
LL r=ub(B+ll,B+1+n,A[i])-B;
int t=lb(C+1,C+1+n,A[i])-C;
ans+=(r-ll)*(t-1);
}
}

namespace IO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
//fread->read

bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if (p1==pend){
p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
if (pend==p1){IOerror=1;return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
inline void read(ll &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
}

using namespace IO;

int main()
{
int T_T=0;
int T; read(T);
while(T--)
{
read(n); ans=0;
for(int i=1;i<=n;i++) read(A[i]),x[A[i]].r++; sort(A+1,A+1+n);
for(int i=1;i<=n;i++) read(B[i]),y[B[i]].r++; sort(B+1,B+1+n);
for(int i=1;i<=n;i++) read(C[i]),z[C[i]].r++; sort(C+1,C+1+n);
int mx=max(A[n],max(B[n],C[n]))<<1; for(l=1;l<mx;l<<=1);
if (n>1000) {FFT(x,l,1); FFT(y,l,1); FFT(z,l,1);}
solve(A,B,C,x,y); solve(B,C,A,y,z); solve(A,C,B,x,z);
for(int i=1;i<=n;i++)
{
int l1=lb(B+1,B+1+n,A[i])-B;
if (A[i]!=B[l1]) continue;
int l2=lb(C+1,C+1+n,A[i])-C;
if (A[i]!=C[l2]) continue;
int r1=ub(B+l1,B+1+n,A[i])-B;
LL r2=ub(C+l2,C+1+n,A[i])-C;
ans+=(r2-l2)*(r1-l1);
}
if (n>1000)
{
memset(x,0,sizeof(x[0])*l);
memset(y,0,sizeof(x[0])*l);
memset(z,0,sizeof(x[0])*l);
}
printf("Case #%d: %lld\n",++T_T,ans);

}
}

 

举报

相关推荐

0 条评论