本文主要是介绍【BNU】33943 Super Rooks on Chessboard 【FFT】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【BNU】33943 Super Rooks on Chessboard
UVA上的题,然而我怎么会蠢到去UVA呢!(其实是百度首先跳出来的是BNU → _ → )
题目分析:
设 numx 为 N 个车没有覆盖的行数,
首先我们考虑没有主对角线覆盖这一条件时,总共的没有被覆盖的面积就是
现在我们考虑主对角线影响。
考虑没有被车覆盖的行的集合 R={r1,r2,r3...rm}
考虑没有被车覆盖的列的集合 C={c1,c2,c3...cn}
考虑被车覆盖的对角线的集合 D={d1,d2,d3...ds}
那么对角线 dk 覆盖的还未被覆盖的格子数量即 ∑∑ri−cj=dk
考虑 FFT ,我们将行集合,列集合转化成多项式: R=∑xri , C=∑x−ci 。
做一遍 FFT 答案就出来了。
心得: FFT 时,首元素可以为任意阶。
my code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std ;typedef long long LL ;#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )const int MAXN = 100005 ;
const double pi = acos ( -1.0 ) ;struct Complex {double r , i ;Complex () {}Complex ( double r , double i ) : r ( r ) , i ( i ) {}Complex operator + ( const Complex& t ) const {return Complex ( r + t.r , i + t.i ) ;}Complex operator - ( const Complex& t ) const {return Complex ( r - t.r , i - t.i ) ;}Complex operator * ( const Complex& t ) const {return Complex ( r * t.r - i * t.i , r * t.i + i * t.r ) ;}
} ;void FFT ( Complex y[] , int n , int rev ) {for ( int i = 1 , j , k , t ; i < n ; ++ i ) {for ( j = 0 , k = n >> 1 , t = i ; k ; k >>= 1 , t >>= 1 ) j = j << 1 | t & 1 ;if ( i < j ) swap ( y[i] , y[j] ) ;}for ( int s = 2 , ds = 1 ; s <= n ; ds = s , s <<= 1 ) {Complex wn ( cos ( rev * 2 * pi / s ) , sin ( rev * 2 * pi / s ) ) , w ( 1 , 0 ) , t ;for ( int k = 0 ; k < ds ; ++ k , w = w * wn ) {for ( int i = k ; i < n ; i += s ) {y[i + ds] = y[i] - ( t = w * y[i + ds] ) ;y[i] = y[i] + t ;}}}if ( rev == -1 ) for ( int i = 0 ; i < n ; ++ i ) y[i].r /= n ;
}bool visx[MAXN] , visy[MAXN] , visd[MAXN << 2] ;
Complex x1[MAXN << 2] , x2[MAXN << 2] ;
int r , c , q , n ;void solve ( int T ) {int x , y , numx = 0 , numy = 0 ;clr ( visx , 0 ) ;clr ( visy , 0 ) ;clr ( visd , 0 ) ;scanf ( "%d%d%d" , &r , &c , &q ) ;for ( int i = 0 ; i < q ; ++ i ) {scanf ( "%d%d" , &x , &y ) ;visx[x] = 1 ;visy[y] = 1 ;visd[x - y + c] = 1 ;}int n = 1 ;while ( n < r + c + 1 ) n <<= 1 ;for ( int i = 1 ; i <= r ; ++ i ) numx += visx[i] == 0 ;for ( int i = 1 ; i <= c ; ++ i ) numy += visy[i] == 0 ;for ( int i = 1 ; i <= r ; ++ i ) x1[i - 1] = Complex ( visx[i] == 0 , 0 ) ;for ( int i = r ; i < n ; ++ i ) x1[i] = Complex ( 0 , 0 ) ;for ( int i = 1 ; i <= c ; ++ i ) x2[c - i] = Complex ( visy[i] == 0 , 0 ) ;for ( int i = c ; i < n ; ++ i ) x2[i] = Complex ( 0 , 0 ) ;FFT ( x1 , n , 1 ) ;FFT ( x2 , n , 1 ) ;for ( int i = 0 ; i < n ; ++ i ) x1[i] = x1[i] * x2[i] ;FFT ( x1 , n , -1 ) ;LL ans = ( LL ) numx * numy ;for ( int i = 0 ; i < n ; ++ i ) ans -= ( LL ) ( x1[i].r + 0.5 ) * visd[i + 1] ;printf ( "Case %d: %lld\n" , T , ans ) ;
}int main () {int T ;scanf ( "%d" , &T ) ;for ( int i = 1 ; i <= T ; ++ i ) solve ( i ) ;return 0 ;
}
这篇关于【BNU】33943 Super Rooks on Chessboard 【FFT】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!