• UVA 12298 Super Poker II (FFT)


    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int N = 1000005;
    const long double pi = acos(-1.0);
    
    struct Complex
    {
        long double r,i;
        Complex(long double r=0, long double i=0):r(r),i(i) {};
        Complex operator+(const Complex &rhs)
        {
            return Complex(r + rhs.r,i + rhs.i);
        }
        Complex operator-(const Complex &rhs)
        {
            return Complex(r - rhs.r,i - rhs.i);
        }
        Complex operator*(const Complex &rhs)
        {
            return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);
        }
    } pS[N], pH[N], pC[N], pD[N];
    //len = 2^M,reverse F[i] with  F[j] j为i二进制反转
    void rader(Complex F[],int len)
    {
        int j = len >> 1;
        for(int i = 1; i < len - 1; ++i)
        {
            if(i < j) swap(F[i],F[j]);  // reverse
            int k = len>>1;
            while(j>=k)
            {
                j -= k;
                k >>= 1;
            }
            if(j < k) j += k;
        }
    }
    
    void FFT(Complex F[],int len,int t)
    {
        rader(F,len);
        for(int h=2; h<=len; h<<=1)
        {
            Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h));
            for(int j=0; j<len; j+=h)
            {
                Complex E(1,0); //旋转因子
                for(int k=j; k<j+h/2; ++k)
                {
                    Complex u = F[k];
                    Complex v = E*F[k+h/2];
                    F[k] = u+v;
                    F[k+h/2] = u-v;
                    E=E*wn;
                }
            }
        }
        if(t==-1)   //IDFT
            for(int i=0; i<len; ++i)
                F[i].r/=len;
    }
    
    void Conv(Complex a[],Complex b[],int len) //求卷积
    {
        FFT(a,len,1);
        FFT(b,len,1);
        for(int i=0; i<len; ++i) a[i] = a[i]*b[i];
        FFT(a,len,-1);
    }
    
    long prime[N] = {0},num_prime = 0;
    int isNotPrime[N] = {1, 1};
    void init()
    {
        for(long i = 2 ; i < N ; i ++)
        {
            if(! isNotPrime[i])
                prime[num_prime ++]=i;
            for(long j = 0 ; j < num_prime && i * prime[j] <  N ; j ++)
            {
                isNotPrime[i * prime[j]] = 1;
                if( !(i % prime[j] ) )
                    break;
            }
        }
    }
    
    
    
    int main()
    {
        int A, B, C;
        init();
        while(scanf("%d%d%d", &A, &B, &C) && (A+B+C))
        {
            memset(pS, 0, sizeof(pS));
            memset(pH, 0, sizeof(pH));
            memset(pC, 0, sizeof(pC));
            memset(pD, 0, sizeof(pD));
            for(int i=2; i<=B; ++i)
                if(isNotPrime[i])
                    pS[i]=pH[i]=pC[i]=pD[i]=Complex(1);
            int len=1;
            while(len<B) len<<=1;
            len<<=3;
            while(C--)
            {
                int v;
                char type;
                scanf("%d%c", &v, &type);
                switch(type)
                {
                    case 'S':pS[v]=Complex(0);break;
                    case 'H':pH[v]=Complex(0);break;
                    case 'C':pC[v]=Complex(0);break;
                    case 'D':pD[v]=Complex(0);break;
                }
            }
            FFT(pS, len, 1), FFT(pH, len, 1), FFT(pC, len, 1), FFT(pD, len, 1);
            for(int i=0; i<len; ++i)
                pS[i]=pS[i]*pH[i]*pC[i]*pD[i];
            FFT(pS, len, -1);
            for(int i=A; i<=B; ++i)
                printf("%lld
    ", (long long)(pS[i].r+0.5));
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    python之matplotlib库中pyplot的基本使用(python数据分析之绘制图形)
    小球称重问题~通过三次称重找出十二个小球质量不一样的小球,并判断小球轻重
    python爬虫—爬取英文名以及正则表达式的介绍
    Python爬取酷狗飙升榜前十首(100)首,写入CSV文件
    Requests库主要方法解析以及Requests库入门需要掌握的框架
    彻底理解Java中的21种锁!
    JavaIO流常见面试题
    Linux常用命令
    语言学习网
    类加载器的命名空间
  • 原文地址:https://www.cnblogs.com/xiepingfu/p/7454333.html
Copyright © 2020-2023  润新知