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:
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:
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #define maxn 131075 7 #define pi 3.14159265358979323846 8 #define mod 1000000 9 using namespace std; 10 typedef long long int64; 11 char ch; 12 int l,r,m,n,x,len,tot,re[maxn],prime[maxn]; 13 bool ok,bo[maxn]; 14 void read(int &x){ 15 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==‘-‘) ok=1; 16 for (x=0;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar()); 17 if (ok) x=-x; 18 } 19 int rev(int v){ 20 int t=0; 21 for (int i=0;i<len;i++) t<<=1,t|=v&1,v>>=1; 22 return t; 23 } 24 struct comp{ 25 double rea,ima; 26 void clear(){rea=ima=0;} 27 comp operator +(const comp &x){return (comp){rea+x.rea,ima+x.ima};} 28 comp operator -(const comp &x){return (comp){rea-x.rea,ima-x.ima};} 29 comp operator *(const comp &x){return (comp){rea*x.rea-ima*x.ima,rea*x.ima+ima*x.rea};} 30 }a[maxn],b[maxn],c[maxn],d[maxn],Wn[2][maxn],wn,w,t1,t2; 31 void fft(comp *a,int op){ 32 for (int i=0,t=re[i];i<n;i++,t=re[i]) if (i<t) swap(a[i],a[t]); 33 for (int s=2;s<=n;s<<=1){ 34 wn=Wn[op][s];//cout<<wn.rea<<‘ ‘<<wn.ima<<endl; 35 for (int i=0;i<n;i+=s){ 36 w=(comp){1,0}; 37 for (int j=i;j<i+(s>>1);j++,w=w*wn){ 38 t1=a[j],t2=w*a[j+(s>>1)]; 39 a[j]=t1+t2,a[j+(s>>1)]=t1-t2; 40 } 41 } 42 } 43 if (op) for (int i=0;i<n;i++) a[i].rea/=n,a[i].ima/=n; 44 } 45 void work(){ 46 for (int i=0;i<=r;i++) a[i].rea=(int64)round(a[i].rea)%mod,a[i].ima=0; 47 for (int i=r+1;i<n;i++) a[i].clear(); 48 } 49 void init(){ 50 for (int i=2;i<maxn;i<<=1) Wn[0][i]=(comp){cos(2*pi/i),sin(2*pi/i)}; 51 for (int i=2;i<maxn;i<<=1) Wn[1][i]=(comp){cos(-2*pi/i),sin(-2*pi/i)}; 52 for (int i=2;i<=50000;i++){ 53 if (!bo[i]) prime[++tot]=i; 54 for (int j=1;j<=tot&&i*prime[j]<=50000;j++){ 55 bo[i*prime[j]]=1; 56 if (!(i%prime[j])) break; 57 } 58 } 59 } 60 int main(){ 61 for (init(),read(l),read(r),read(m);l&&r;read(l),read(r),read(m)){ 62 for (len=0,n=1;n<((r+1)<<1);len++,n<<=1); 63 for (int i=0;i<n;i++) re[i]=rev(i); 64 for (int i=0;i<n;i++) a[i].clear(),b[i].clear(),c[i].clear(),d[i].clear(); 65 for (int i=2;i<r;i++) a[i].rea=b[i].rea=c[i].rea=d[i].rea=bo[i]; 66 for (int i=1;i<=m;i++){ 67 read(x); 68 if (ch==‘S‘) a[x].rea=0; 69 else if (ch==‘H‘) b[x].rea=0; 70 else if (ch==‘C‘) c[x].rea=0; 71 else if (ch==‘D‘) d[x].rea=0; 72 } 73 fft(a,0),fft(b,0),fft(c,0),fft(d,0); 74 for (int i=0;i<n;i++) a[i]=a[i]*b[i]; 75 fft(a,1),work(),fft(a,0); 76 for (int i=0;i<n;i++) a[i]=a[i]*c[i]; 77 fft(a,1),work(),fft(a,0); 78 for (int i=0;i<n;i++) a[i]=a[i]*d[i]; 79 fft(a,1),work(); 80 for (int i=l;i<=r;i++) printf("%d\n",(int)a[i].rea); 81 puts(""); 82 } 83 return 0; 84 }