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:

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 }
时间: 2024-11-10 18:14:45

bzoj2487: Super Poker II的相关文章

UVa12298 Super Poker II(母函数 + FFT)

题目 Source http://acm.hust.edu.cn/vjudge/problem/23590 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),

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),

浅谈FFT(快速傅里叶变换)

本文主要简单写写自己学习FFT的经历以及一些自己的理解和想法. FFT的介绍以及入门就不赘述了,网上有许多相关的资料,入门的话推荐这篇博客:FFT(最详细最通俗的入门手册),里面介绍得很详细. 为什么要学习FFT呢?因为FFT能将多项式乘法的时间复杂度由朴素的$O(n^2)$降到$O(nlogn)$,这相当于能将任意形如$f[k]=\sum\limits _{i+j=k}f[i]*f[j]$的转移方程的计算在$O(nlogn)$的时间内完成.因此对于想要进阶dp的同学来说,FFT是必须掌握的技能

java基础知识总结1

一. java开发工具 Editplus:Editplus与jdk的连接配置,如:javac(编译).java(运行).javap(查看字节码) a) 配置内容: i. javac(编译):参数为 -d . (FileName):初始目录(FileDir) ii. java(运行):参数为 (CurSel).(FileNameNoExt):初始目录:(FileDir)iii.javap(查看字节码):参数为(FileNameNoExt):初始目录:$(FileDir) 二. java初始 : a

ssh整合使用struts-spring-pluing

一.创建整合的项目    1.项目名称:spring101503    2.在项目中创建conf目录    3.在项目中创建test目录二,添加struts支持(struts版本2.3.7)    1.struts2 2.3.7.jar        1).使用核心jar文件            asm-3.3.jar            asm-commons-3.3.jar            asm-tree-3.3.jar            commons-fileupload

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

INSTALLING QUARTUS II V.13.1 64 BIT ON RHEL/CENTOS 6 64 BIT

http://www.digitalsolutionslab.com/installing-quartus-ii-v-13-1-64-bit-on-rhelcentos-6-64-bit/ I have been using Quartus II v.12.1 on RHEL 5 and decided that going through the installation procedure for the Quartus II v.13.1 on an updated RHEL (namel

Super Ugly Number

题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first

LeetCode Super Ugly Number

原题链接在这里:https://leetcode.com/problems/super-ugly-number/ 题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8,