UVA 12633 Super Rooks on Chessboard [fft 生成函数]

Super Rooks on Chessboard

UVA - 12633

题意:

超级车可以攻击行、列、主对角线3 个方向。
R * C 的棋盘上有N 个超级车,问不被攻击的格子总数。


行列好好做啊,就是不被攻击的行数*列数

减去主对角线的,就是不被攻击的行列中求\(r - c = d\)的三元组个数

考虑写出行和列生成函数 \(A(x)=\sum x^{r_i},B(x)=\sum x^{-c_i}\),乘起来就行了

可以乘上\(x^c\)来避免负指数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = (1<<17) + 5;
const double PI = acos(-1.0);
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 meow{
    double x, y;
    meow(double a=0, double b=0):x(a), y(b){}
};
meow operator +(meow a, meow b) {return meow(a.x+b.x, a.y+b.y);}
meow operator -(meow a, meow b) {return meow(a.x-b.x, a.y-b.y);}
meow operator *(meow a, meow b) {return meow(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x);}
meow conj(meow a) {return meow(a.x, -a.y);}
typedef meow cd;

namespace fft {
	int n, rev[N];
	cd omega[N], omegaInv[N];
	void init(int lim) {
		n = 1; int k = 0; while(n < lim) n <<= 1, k++;
		for(int i=0; i<n; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1));

		for(int i=0; i<n; i++) {
			omega[i] = cd(cos(2*PI/n*i), sin(2*PI/n*i));
			omegaInv[i] = conj(omega[i]);
		}
	}

	void dft(cd *a, int flag) {
		cd *w = flag == 1 ? omega : omegaInv;
		for(int i=0; i<n; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
		for(int l=2; l<=n; l<<=1) {
			int m = l>>1;
			for(cd *p = a; p != a+n; p += l) 
				for(int k=0; k<m; k++) {
					cd t = w[n/l*k] * p[k+m];
					p[k+m] = p[k] - t;
					p[k] = p[k] + t;
				}
		}
		if(flag == -1) for(int i=0; i<n; i++) a[i].x /= n;
	}

	void mul(cd *a, cd *b) {
		dft(a, 1); dft(b, 1);
		for(int i=0; i<n; i++) a[i] = a[i] * b[i];
		dft(a, -1);
	}
}

int r, c, n, m, R[N], C[N], D[N], x, y;
cd a[N], b[N];
int main() {
	freopen("in", "r", stdin);
	int T = read(), cas = 0;
	while(T--) {
		r = read(); c = read(); n = read(); m = max(r, c);
		fft::init(m+m+1);
		for(int i=0; i<fft::n; i++) a[i] = b[i] = cd(), R[i] = C[i] = D[i] = 0;
		for(int i=1; i<=n; i++) 
			x=read(), y=read(), R[x] = 1, C[y] = 1, D[x+m-y] = 1; 
		int tr = 0, tc = 0;
		for(int i=1; i<=r; i++) if(!R[i]) tr++, a[i].x = 1.0;
		for(int i=1; i<=c; i++) if(!C[i]) tc++, b[m-i].x = 1.0;
		ll ans = (ll) tr * tc, t = 0; 
		fft::mul(a, b);
		for(int i=0; i <= m<<1; i++) if(D[i]) t += (ll) floor(a[i].x + 0.5);
		printf("Case %d: %lld\n", ++cas, ans - t);
	}
}

posted @ 2017-04-22 21:50  Candy?  阅读(425)  评论(0编辑  收藏  举报