题意:给定一个区间[a,b],给定四种花色的纸牌,每张牌上都有一个合数.对于每一个n属于[a,b]四种牌中各取一张和等于n的方案数.然后限制条件是有几种牌遗失了.
裸的fft,找出所有的合数记录下来然后去掉遗失的,对每个花色FFT,然后四个花色的点表达式相乘求出IDFT就是方案数了~
坑点:所有的都要换成long double!!!
#include <cstring> #include <cstdio> #include <iostream> #include <cmath> #include <algorithm> using namespace std; const long double pi = acos (-1); #define maxn 1<<19 struct plex { long double x, y; plex (long double _x = 0.0, long double _y = 0.0) : x (_x), y (_y) {} plex operator + (const plex &a) const { return plex (x+a.x, y+a.y); } plex operator - (const plex &a) const { return plex (x-a.x, y-a.y); } plex operator * (const plex &a) const { return plex (x*a.x-y*a.y, x*a.y+y*a.x); } }; 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; } plex A[1 << 19]; void FFT(plex *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); plex wm = plex(cos(DFT*2*pi/m), sin(DFT*2*pi/m)); for(int k = 0; k < len; k += m) { plex w = plex(1, 0); for(int j = 0; j < (m >> 1); j++) { plex t = w*A[k + j + (m >> 1)]; plex 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].x /= len, A[i].y /= len; for(int i = 0; i < len; i++) a[i] = A[i]; return; } char op[11111][11]; bool is_prime[51111]; int a, b, c, d; plex S[maxn], H[maxn], D[maxn], C[maxn]; void init () { memset (is_prime, 0, sizeof is_prime); for (int i = 2; i <= 50000; i++) { if (!is_prime[i]) { for (int j = i+i; j <= 50000; j += i) is_prime[j] = 1; } } } int main () { //freopen ("in.txt", "r", stdin); 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(is_prime[i]) { S[i] = H[i] = C[i] = D[i] = plex(1, 0); } else S[i] = H[i] = C[i] = D[i] = plex(0, 0); for(int i = b + 1; i < len; i++) S[i] = H[i] = C[i] = D[i] = plex(0, 0); int num; char type; for(int i = 0; i < c; i++) { scanf("%d%c", &num, &type); switch(type) { case 'S': S[num] = plex(0, 0); break; case 'H': H[num] = plex(0, 0); break; case 'C': C[num] = plex(0, 0); break; case 'D': D[num] = plex(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", (long long)(S[i].x + 0.5)); putchar('\n'); } return 0; }