题目大意:
就是现在有一堆扑克里面的牌有无数张, 每种合数的牌有4中不同花色各一张(0, 1都不是合数), 没有质数或者大小是0或者1的牌
现在这堆牌中缺失了其中的 c 张牌, 告诉你a, b, c接下来c张不同的丢失的牌, 然后求从这堆牌中拿出各种花色的牌各一张, 得到的点数和是k的种数有多少种(一种组合算作一种), 需要全部所有的a <= k <= b的k对应的结果
大致思路:
这个题是个很明显的FFT问题, 用x^k前的系数表示大小为k的牌有还是没有(1还是0), 一共4个多项式相乘即可, 得到结果的多项式中x^k前的系数代表的就是可以的方案数
另外注意限制多项式次数 b <= 50000故每个多项式可以用 1 << 16以内的数表示, 但是需要求4个多项式的乘积于是多项式表示出来时要开到 1 << 19
这个题比较坑的就是double的精度不够....我换了long double 才过...
代码如下:
Result : Accepted Memory : 0 KB Time : 2413 ms
/*
* Author: Gatevin
* Created Time: 2015/7/15 15:58:33
* File Name: UVA12298.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
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;
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\n", (lint)(S[i].real + 0.5));
putchar('\n');
}
return 0;
}