zoukankan      html  css  js  c++  java
  • UVA 12298 Super Poker II(FFT+母函数)


    题目大意:

    就是现在有一堆扑克里面的牌有无数张, 每种合数的牌有4中不同花色各一张(0, 1都不是合数), 没有质数或者大小是0或者1的牌
    现在这堆牌中缺失了其中的 c 张牌, 告诉你a, b, c接下来c张不同的丢失的牌, 然后求从这堆牌中拿出各种花色的牌各一张,
    得到的点数和是k的种数有多少种(一种组合算作一种), 需要全部所有的a <= k <= b的k对应的结果


    做法:FFT

    技巧:
    我们可以把它变成多项式,根据卷积性质,求得计数。
    用x^k前的系数表示大小为k的牌有还是没有(1还是0), 一共4个多项式相乘即可, 得到结果的多项式中x^k前的系数代表的就是可以的方案数
    因为我们用卷积(0,1,2,0多项式0是第一项),4个多项式相乘后,第len个位置的值是从4个和为len的地方来

    所以可以完成统计a<->b的和的数量

     注意精度问题


     附上代码:

    #include<bits/stdc++.h> 
    using namespace std;
    const double eps(1e-8);
    typedef long long lint;
    
    const long double PI = acos(-1.0);
    
    bool isPrime[50010];
    bool check[50010];
    
    void init()
    {
        memset(isPrime, 0, sizeof(isPrime));
        memset(check, 0, sizeof(check));
        isPrime[0] = isPrime[1] = 1;
        check[0] = check[1] = 1;
        for(int i = 2; i <= 50000; i++)
        {
            if(check[i]) continue;
            isPrime[i] = 1;
            for(int j = i; j <= 50000; j += i)
                check[j] = 1;
        }
        return;
    }
    
    struct Complex
    {
        long double real, image;
        Complex(long double _real, long double _image)
        {
            real = _real;
            image = _image;
        }
        Complex(){}
    };
    
    Complex operator + (const Complex &c1, const Complex &c2)
    {
        return Complex(c1.real + c2.real, c1.image + c2.image);
    }
    
    Complex operator - (const Complex &c1, const Complex &c2)
    {
        return Complex(c1.real - c2.real, c1.image - c2.image);
    }
    
    Complex operator * (const Complex &c1, const Complex &c2)
    {
        return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
    }
    
    int rev(int id, int len)
    {
        int ret = 0;
        for(int i = 0; (1 << i) < len; i++)
        {
            ret <<= 1;
            if(id & (1 << i)) ret |= 1;
        }
        return ret;
    }
    
    Complex A[1 << 19];
    void FFT(Complex *a, int len, int DFT)
    {
        for(int i = 0; i < len; i++)
            A[rev(i, len)] = a[i];
        for(int s = 1; (1 << s) <= len; s++)
        {
            int m = (1 << s);
            Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
            for(int k = 0; k < len; k += m)
            {
                Complex w = Complex(1, 0);
                for(int j = 0; j < (m >> 1); j++)
                {
                    Complex t = w*A[k + j + (m >> 1)];
                    Complex u = A[k + j];
                    A[k + j] = u + t;
                    A[k + j + (m >> 1)] = u - t;
                    w = w*wm;
                }
            }
        }
        if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
        for(int i = 0; i < len; i++) a[i] = A[i];
        return;
    }
    
    Complex S[1 << 19], H[1 << 19], C[1 << 19], D[1 << 19];
    
    int main()
    {
        int a, b, c;
        init();
        while(scanf("%d %d %d", &a, &b, &c), a || b || c)
        {
            int len = 1;
            while(len <= b) len <<= 1;
            len <<= 3;//好像因为有4个多项式 就要多开一点 
            for(int i = 0; i <= b; i++)
                if(!isPrime[i]) S[i] = H[i] = C[i] = D[i] = Complex(1, 0);
                else S[i] = H[i] = C[i] = D[i] = Complex(0, 0);
            for(int i = b + 1; i < len; i++) S[i] = H[i] = C[i] = D[i] = Complex(0, 0);
            int num;
            char type;
            for(int i = 0; i < c; i++)
            {
                scanf("%d%c", &num, &type);
                switch(type)
                {
                    case 'S': S[num] = Complex(0, 0); break;
                    case 'H': H[num] = Complex(0, 0); break;
                    case 'C': C[num] = Complex(0, 0); break;
                    case 'D': D[num] = Complex(0, 0); break;
                }
            }
            FFT(S, len, 1); FFT(H, len, 1); FFT(C, len, 1); FFT(D, len, 1);
            for(int i = 0; i < len; i++)
                S[i] = S[i]*H[i]*C[i]*D[i];
            FFT(S, len, -1);
            for(int i = a; i <= b; i++)
                printf("%lld
    ", (lint)(S[i].real + 0.5));
            putchar('
    ');
        }
        return 0;
    }

  • 相关阅读:
    C++PRIMER PLUS第六版课后编程答案 5.1-5.5
    C++PRIMER PLUS第六版课后编程答案 4.6-4.10
    C++PRIMER PLUS第六版课后编程答案 4.1-4.5
    C++PRIMER PLUS第六版课后编程题答案 3.1-3.7
    C++ Primer Plus 第六版课后编程答案 2.6-2.7
    C++ Primer Plus 第六版课后编程答案 2.1-2.5
    jmf天昏地暗之路(三)---抓取当前帧照片并保存为bmp格式(结束)
    win7下chm文件无法正常显示所示
    HDOJ 2929 Bigger is Better
    Codeforces Round #202 (Div. 2) A,B
  • 原文地址:https://www.cnblogs.com/Heey/p/9054784.html
Copyright © 2011-2022 走看看