bzoj2487: Super Poker II

时间:2023-03-09 16:02:50
bzoj2487: Super Poker II

Description

I have a set of super poker cards, consisting of an infinite number of cards. For each positive composite integer p, there
are exactly four cards whose value is p: Spade(S), Heart(H), Club(C) and
Diamond(D). There are no cards of other values.
By “composite integer”, we
mean integers that have more than 2 divisors. For example, 6 is a composite
integer, since it
has 4 divisors: 1, 2, 3, 6; 7 is not a composite number,
since 7 only has 2 divisors: 1 and 7. Note that 1 is not composite
(it has
only 1 divisor).
 
Given a positive integer n, how many ways can you pick
up exactly one card from each suit (i.e. exactly one spade card,
one heart
card, one club card and one diamond card), so that the card values sum to n? For
example, if n=24, one way is
4S+6H+4C+10D, shown below:

bzoj2487: Super Poker II

Unfortunately, some of the cards are lost,
but this makes the problem more interesting. To further make the problem even

more interesting (and challenging!), I’ll give you two other positive
integers a and b, and you need to find out all the
answers for n=a, n=a+1,
…, n=b.

Input

The input contains at most 25 test cases.
Each test case begins with 3 integers a, b and c, where c is the number of lost

cards. The next line contains c strings, representing the lost cards. Each
card is formatted as valueS, valueH, valueC or
valueD, where value is a
composite integer. No two lost cards are the same. The input is terminated by
a=b=c=0. There
will be at most one test case where a=1, b=50,000 and
c<=10,000. For other test cases, 1<=a<=b<=100,
0<=c<=10.

Output

For each test case, print b-a+1 integers, one
in each line. Since the numbers might be large, you should output each

integer modulo 1,000,000. Print a blank line after each test
case.

Sample Input

12 20 2
4S 6H
0 0 0

Sample Output

0
0
0
0
0
0
1

0
3

HINT

很简单的fft,看懂题面即可。

code:

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 131075
#define pi 3.14159265358979323846
#define mod 1000000
using namespace std;
typedef long long int64;
char ch;
int l,r,m,n,x,len,tot,re[maxn],prime[maxn];
bool ok,bo[maxn];
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
int rev(int v){
int t=;
for (int i=;i<len;i++) t<<=,t|=v&,v>>=;
return t;
}
struct comp{
double rea,ima;
void clear(){rea=ima=;}
comp operator +(const comp &x){return (comp){rea+x.rea,ima+x.ima};}
comp operator -(const comp &x){return (comp){rea-x.rea,ima-x.ima};}
comp operator *(const comp &x){return (comp){rea*x.rea-ima*x.ima,rea*x.ima+ima*x.rea};}
}a[maxn],b[maxn],c[maxn],d[maxn],Wn[][maxn],wn,w,t1,t2;
void fft(comp *a,int op){
for (int i=,t=re[i];i<n;i++,t=re[i]) if (i<t) swap(a[i],a[t]);
for (int s=;s<=n;s<<=){
wn=Wn[op][s];//cout<<wn.rea<<' '<<wn.ima<<endl;
for (int i=;i<n;i+=s){
w=(comp){,};
for (int j=i;j<i+(s>>);j++,w=w*wn){
t1=a[j],t2=w*a[j+(s>>)];
a[j]=t1+t2,a[j+(s>>)]=t1-t2;
}
}
}
if (op) for (int i=;i<n;i++) a[i].rea/=n,a[i].ima/=n;
}
void work(){
for (int i=;i<=r;i++) a[i].rea=(int64)round(a[i].rea)%mod,a[i].ima=;
for (int i=r+;i<n;i++) a[i].clear();
}
void init(){
for (int i=;i<maxn;i<<=) Wn[][i]=(comp){cos(*pi/i),sin(*pi/i)};
for (int i=;i<maxn;i<<=) Wn[][i]=(comp){cos(-*pi/i),sin(-*pi/i)};
for (int i=;i<=;i++){
if (!bo[i]) prime[++tot]=i;
for (int j=;j<=tot&&i*prime[j]<=;j++){
bo[i*prime[j]]=;
if (!(i%prime[j])) break;
}
}
}
int main(){
for (init(),read(l),read(r),read(m);l&&r;read(l),read(r),read(m)){
for (len=,n=;n<((r+)<<);len++,n<<=);
for (int i=;i<n;i++) re[i]=rev(i);
for (int i=;i<n;i++) a[i].clear(),b[i].clear(),c[i].clear(),d[i].clear();
for (int i=;i<r;i++) a[i].rea=b[i].rea=c[i].rea=d[i].rea=bo[i];
for (int i=;i<=m;i++){
read(x);
if (ch=='S') a[x].rea=;
else if (ch=='H') b[x].rea=;
else if (ch=='C') c[x].rea=;
else if (ch=='D') d[x].rea=;
}
fft(a,),fft(b,),fft(c,),fft(d,);
for (int i=;i<n;i++) a[i]=a[i]*b[i];
fft(a,),work(),fft(a,);
for (int i=;i<n;i++) a[i]=a[i]*c[i];
fft(a,),work(),fft(a,);
for (int i=;i<n;i++) a[i]=a[i]*d[i];
fft(a,),work();
for (int i=l;i<=r;i++) printf("%d\n",(int)a[i].rea);
puts("");
}
return ;
}