zoukankan      html  css  js  c++  java
  • UVA

    题意:一个N*M的棋盘上,放置N个皇后,皇后(i,j)可以攻击整行整列和两条对角线.求放完这N个皇后,棋盘上还有多少个点不会被攻击到.
    分析:除了行和列之外,还要考虑对角线.对于每一个格点((x,y)),都有其对应的主对角线(x+M-j)(保证编号>0).如果行i和列j都不会被占据,那么点((i,j))就会对其主对角线做出贡献,当然,需要保证这条对角线上没有皇后.将主对角线视作(A^k),则(A^k的系数为sum_{i+M-j=k}R^i*C^i).若i行没有被占据,则(R^i = 1),否则为0, 若j列没被占据,(C^{M-j})为1,否则为0.
    求出卷积后,若对角线k上没有皇后,则该对角线的系数对答案做出贡献.

    #include <bits/stdc++.h>	
    using namespace std;
    typedef long long LL;
    const int MAXN = 4e5 + 10;
    const double PI = acos(-1.0);
    struct Complex{
        double x, y;
        inline Complex operator+(const Complex b) const {
            return (Complex){x +b.x,y + b.y};
        }
        inline Complex operator-(const Complex b) const {
            return (Complex){x -b.x,y - b.y};
        }
        inline Complex operator*(const Complex b) const {
            return (Complex){x *b.x -y * b.y,x * b.y + y * b.x};
        }
    } va[MAXN * 2 + MAXN / 2], vb[MAXN * 2 + MAXN / 2];
    int lenth = 1, rev[MAXN * 2 + MAXN / 2];
    int N, M;   // f 和 g 的数量
        // f g和 的系数
        // 卷积结果
        // 大数乘积
    int f[MAXN],g[MAXN];
    vector<LL> conv;
    vector<LL> multi;
    //f g
    void init()
    {
        int tim = 0;
        lenth = 1;
        conv.clear(), multi.clear();
        memset(va, 0, sizeof va);
        memset(vb, 0, sizeof vb);
        while (lenth <= N + M - 2)
            lenth <<= 1, tim++;
        for (int i = 0; i < lenth; i++)
            rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (tim - 1));
    }
    
    void FFT(Complex *A, const int fla)
    {
        for (int i = 0; i < lenth; i++){
            if (i < rev[i]){
                swap(A[i], A[rev[i]]);
            }
        }
        for (int i = 1; i < lenth; i <<= 1){
            const Complex w = (Complex){cos(PI / i), fla * sin(PI / i)};
            for (int j = 0; j < lenth; j += (i << 1)){
                Complex K = (Complex){1, 0};
                for (int k = 0; k < i; k++, K = K * w){
                    const Complex x = A[j + k], y = K * A[j + k + i];
                    A[j + k] = x + y;
                    A[j + k + i] = x - y;
                }
            }
        }
    }
    void getConv(){             //求多项式
        init();
        for (int i = 0; i < N; i++)
            va[i].x = f[i];
        for (int i = 0; i < M; i++)
            vb[i].x = g[i];
        FFT(va, 1), FFT(vb, 1);
        for (int i = 0; i < lenth; i++)
            va[i] = va[i] * vb[i];
        FFT(va, -1);
        for (int i = 0; i <= N + M - 2; i++)
            conv.push_back((LL)(va[i].x / lenth + 0.5));
    }
    
    int row[MAXN], col[MAXN];
    int vis[MAXN];
    
    int main()
    {
        #ifndef ONLINE_JUDGE
            freopen("in.txt","r",stdin);
            freopen("out.txt","w",stdout);
        #endif
        int T,cas=1; scanf("%d",&T);
        while(T--){
            int R,C,n; scanf("%d %d %d",&R, &C, &n);
            int x,y;
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=R;++i) row[i] = 1;
            for(int i=1;i<=C;++i) col[C-i] = 1;
            for(int i=1;i<=n;++i){
                scanf("%d %d",&x, &y);
                vis[x+C-y] = true;                  //表示该对角线被占据
                row[x] = 0, col[C-y] = 0;           //行列被占据
            }
            N = R+1;
            M = C;                                 //没被占据的行列做卷积,求合法的对角线
            for(int i=0;i<N;++i) f[i] = row[i];
            for(int i=0;i<M;++i) g[i] = col[i];
            getConv();
            int sz = conv.size();
            LL ans=0;
            for(int i=0;i<sz;++i){
                if(!vis[i])
                    ans+= conv[i];          //若该对角线没被占据,则统计
            }
            printf("Case %d: %lld
    ",cas++,ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    c 概念详解
    c 目录
    win文件操作
    使用panads处理数据
    冒泡排序
    notepad++搭建python环境
    继承方法-->一级一级继承
    原型问题2—原型对象的替换
    原型问题1—原型对象的替换
    js继承——扩展Object方式实现继承
  • 原文地址:https://www.cnblogs.com/xiuwenli/p/9729120.html
Copyright © 2011-2022 走看看