BZOJ 2024: [SHOI2009] 舞会 [容斥原理 高精度]

题意:和上题基本一样,求至少k对a>b的方案数。不取模!!!


做k+1遍容斥就行了

高精度超强!!!几乎把所有的都用上了
然后,注意有负数,所以容斥的时候正负分别保存然后再一减就行了


~~这是我省选前最后一次写高精度了~~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=205, B=1e4;
inline int read(){
	char c=getchar();int x=0,f=1;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}

struct Big{
	int a[120], n;
	int& operator [](int x) {return a[x];}
	Big():n(1) {memset(a, 0, sizeof(a));}
	void ini(int x) {a[1]=x; n=1;}
}f[N][N], C[N][N], fac[N];

Big operator *(Big a, int b) {
	int g=0;
	for(int i=1; i<=a.n; i++) 
		g += a[i]*b, a[i] = g%B, g/=B;
	if(g) a[++a.n] = g;
	return a;
}

Big operator *(Big a, Big b) {
	Big c;
	for(int i=1; i<=a.n; i++) {
		int g=0;
		for(int j=1; j<=b.n; j++) 
			g += c[i+j-1]+a[i]*b[j], c[i+j-1] = g%B, g/=B;
		c[i+b.n] = g;
	}
	c.n = a.n + b.n;
	while(c.n>1 && c[c.n]==0) c.n--;
	return c;
}

Big operator +(Big a, Big b) {
	int g=0, n=max(a.n, b.n);
	for(int i=1; i<=n; i++) {
		g += i<=a.n ? a[i] : 0;
		g += i<=b.n ? b[i] : 0;
		a[i] = g%B, g/=B;
	}
	a.n = n;
	if(g) a[++a.n] = g;
	return a;
}

Big operator -(Big a, Big b) {
	for(int i=1; i<=b.n; i++) {
		if(a[i]<b[i]) a[i]+=B, a[i+1]--;
		a[i] -= b[i];
	}
	int p=b.n+1;
	while(a[p]<0) a[p]+=B, a[++p]--;
	while(a.n>1 && a[a.n]==0) a.n--;
	return a;
}

void Print(Big &a) {
	printf("%d", a[a.n]);
	for(int i=a.n-1; i>=1; i--) printf("%04d", a[i]);
}


int n, k, a[N], b[N];
int g[N];
void dp() {
	fac[0].ini(1);
	for(int i=1; i<=n; i++) fac[i] = fac[i-1]*i;
	C[0][0].ini(1);
	for(int i=1; i<=n; i++) {
		C[i][0].ini(1);
		for(int j=1; j<=n; j++) C[i][j] = C[i-1][j] + C[i-1][j-1];
	}
	int now=0;
	for(int i=1; i<=n; i++) {
		while(now<n && a[i]>b[now+1]) now++;
		g[i] = now;
	}
	for(int i=0; i<=n; i++) f[i][0].ini(1);
	for(int i=1; i<=n; i++)
		for(int j=1; j<=g[i]; j++) f[i][j] = f[i-1][j] + f[i-1][j-1]*(g[i]-j+1);// Print(f[i][j]), puts("");
}
int main() {
	freopen("in","r",stdin);
	n=read(); k=read();
	for(int i=1; i<=n; i++) b[i]=read();
	for(int i=1; i<=n; i++) a[i]=read();
	sort(a+1, a+1+n); sort(b+1, b+1+n);
	dp();
	Big pos, neg, ans;
	int kkk=k;
	for(int k=0; k<=kkk; k++) { //printf("kkkkkk %d\n",k);
		for(int i=k; i<=n; i++){ //printf("i %d\n",i);
			Big t = fac[n-i] * f[n][i] * C[i][k];  //printf("t ");Print(t); puts("");
			if((i-k)&1) neg = neg + t;
			else pos = pos + t;
		}
	}
	ans = pos - neg;
			//printf("hi %d  ",i),Print(ans), puts(""); 
	Print(ans);
}

posted @ 2017-03-25 16:45  Candy?  阅读(473)  评论(0编辑  收藏  举报