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.

题解:生成函数+FFT优化多项式乘法.

对于每种牌,构造一个生成函数,4 个生成函数相乘,输出对应项数即可.

#include<bits/stdc++.h>
#define maxn 1000000
#define ll long long
#define double long double
#define setIO(s) freopen(s".in","r",stdin), freopen(s".out","w",stdout)
using namespace std; struct cpx
{
double x,y;
cpx(double a=0,double b=0){x=a,y=b;}
};
cpx operator+(cpx a,cpx b) { return cpx(a.x+b.x,a.y+b.y); }
cpx operator-(cpx a,cpx b) { return cpx(a.x-b.x,a.y-b.y); }
cpx operator*(cpx a,cpx b) { return cpx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x); } namespace FFT
{
const double pi=acos(-1);
void FFT(cpx *a,int n,int flag)
{
for(int i = 0,k = 0;i < n; ++i)
{
if(i > k) swap(a[i],a[k]);
for(int j = n >> 1;(k^=j)<j;j>>=1);
}
for(int mid=1;mid<n;mid<<=1)
{
cpx wn(cos(pi/mid),flag*sin(pi/mid)),x,y;
for(int j=0;j<n;j+=(mid<<1))
{
cpx w(1,0);
for(int k=0;k<mid;++k)
{
x = a[j+k],y=w*a[j+mid+k];
a[j+k]=x+y;
a[j+mid+k]=x-y;
w=w*wn;
}
}
}
}
}; cpx arr[maxn], brr[maxn], crr[maxn], drr[maxn];
int vis[maxn],prime[maxn],non_prime[maxn];
int tot,cas;
char str[100];
int idx(char c)
{
if(c=='S') return 0;
if(c=='H') return 1;
if(c=='C') return 2;
if(c=='D') return 3;
}
void update()
{
scanf("%s",str+1);
int len=strlen(str+1);
int num=0;
for(int i=1;i<=len;++i)
{
if(str[i]>='0' && str[i]<='9')
num=num*10+str[i]-'0';
else
{
int cur=idx(str[i]);
switch(cur)
{
case 0 : { arr[num].x=0; break;}
case 1 : { brr[num].x=0; break;}
case 2 : { crr[num].x=0; break;}
case 3 : { drr[num].x=0; break;}
}
}
}
}
void get_number()
{
for(int i=2;i<=100000;++i)
{
if(!vis[i]) prime[++tot]=i;
for(int j=1;j<=tot&&prime[j]*i*1ll<=1ll*100000;++j)
{
vis[prime[j]*i]=1;
if(i%prime[j]==0) break;
}
}
tot=0;
for(int i=2;i<=100000;++i) if(vis[i]) non_prime[++tot]=i;
}
int main()
{
// setIO("input");
get_number();
for(cas=1; ;++cas)
{
int l,r,o,len=1;
scanf("%d%d%d",&l,&r,&o); if(l==0&&r==0&&o==0) break; while(len<=r) len<<=1; len<<=2;
for(int i=1;i<=r;++i) arr[i]=cpx(vis[i],0);
for(int i=1;i<=r;++i) brr[i]=cpx(vis[i],0);
for(int i=1;i<=r;++i) crr[i]=cpx(vis[i],0);
for(int i=1;i<=r;++i) drr[i]=cpx(vis[i],0);
while(o--) update();
// for(int i=0;i<len;++i) printf("%d %d\n",(int)arr[i].x,(int)arr[i].y);
FFT::FFT(arr,len, 1), FFT::FFT(brr,len, 1), FFT::FFT(crr,len, 1), FFT::FFT(drr,len, 1);
for(int i=0;i<len;++i)
{
arr[i]=arr[i]*brr[i]*crr[i]*drr[i];
}
FFT::FFT(arr,len,-1);
for(int i=l;i<=r;++i) printf("%lld\n", (ll)(arr[i].x/len+0.1)%1000000);
printf("\n");
for(int i=0;i<=len+233;++i) arr[i]=brr[i]=crr[i]=drr[i]=cpx(0,0);
}
return 0;
}

  

Super Poker II UVA - 12298 FFT_生成函数的更多相关文章

  1. UVA - 12298 Super Poker II NTT

    UVA - 12298 Super Poker II NTT 链接 Vjudge 思路 暴力开个桶,然后统计,不过会T,用ntt或者fft,ntt用个大模数就行了,百度搜索"NTT大模数&q ...

  2. UVa12298 Super Poker II(母函数 + FFT)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/23590 Description I have a set of super poker cards, ...

  3. bzoj2487: Super Poker II

    Description I have a set of super poker cards, consisting of an infinite number of cards. For each p ...

  4. UVA - 12298 Super Poker II (FFT+母函数)

    题意:有四种花色的牌,每种花色的牌中只能使用数值的约数个数大于2的牌.现在遗失了c张牌.每种花色选一张,求值在区间[a,b]的每个数值的选择方法有多少. 分析:约数个数大于2,即合数.所以先预处理出5 ...

  5. UVA 12298 Super Poker II (FFT)

    #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using ...

  6. FFT(快速傅里叶变换):UVAoj 12298 - Super Poker II

    题目:就是现在有一堆扑克里面的牌有无数张, 每种合数的牌有4中不同花色各一张(0, 1都不是合数), 没有质数或者大小是0或者1的牌现在这堆牌中缺失了其中的 c 张牌, 告诉你a, b, c接下来c张 ...

  7. UVA12298 Super Poker II

    怎么又是没人写题解的UVA好题,个人感觉应该是生成函数的大板子题了. 直接做肯定爆炸,考虑来一发优化,我们记一个多项式,其中\(i\)次项的系数就表示对于\(i\)这个数有多少种表示方式. 那么很明显 ...

  8. GCD - Extreme (II) UVA - 11426(欧拉函数!!)

    G(i) = (gcd(1, i) + gcd(2, i) + gcd(3, i) + .....+ gcd(i-1, i)) ret = G(1) + G(2) + G(3) +.....+ G(n ...

  9. GCD - Extreme (II) UVA - 11426 数学

    Given the value of N , you will have to nd the value of G . The de nition of G is given below: G = i ...

随机推荐

  1. 洛谷—— P1379 八数码难题

    https://daniu.luogu.org/problem/show?pid=1379 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示 ...

  2. 学习KNN算法体会和总结

    k-d树(k-dimensional树的简称),是一种切割k维数据空间的数据结构.主要应用于多维空间重要数据的搜索(如:范围搜索和近期邻搜索). 索引结构中相似性查询有两种主要的方式:一种是范围查询( ...

  3. HDU 4516

    此题不难,但我就是RE,搞不懂啊...郁闷. 说下基本算法吧,只要留意到要分解的因式是(x+ai)..的形式,x前是系数为1的,而且,它们的绝对值在1000以内,于是,好办了.只要枚举(x+k)中的k ...

  4. vim快速操作

    简明 VIM 练级攻略 vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn ...

  5. [Debug] Use Remote Sources to Debug a Web App on an Emulator, Simulator, or Physical Device

    We can emulate different operating systems, browsers, and devices within a desktop operating system. ...

  6. 成都传智播客java就业班激情洋溢的青春篮球赛

    为了缓解学员们的学习压力,也为了培养学员们的团队协作精神,5月28日下午,在班主任倪老师和王老师联手带领下,我们1406280ls" style="color:rgb(51,102 ...

  7. python 003 os模块 example

    import os for tmpdir in ('/tmp',r'C:/Users/Administrator/PycharmProjects/'): if os.path.isdir(tmpdir ...

  8. SQL语句改动表名和字段名

    今天有个暂时任务,改动生产环境的数据库表名和字段名.曾经要改动表名字段名都是在开发环境.直接打开 SQL Server找到相应的表或字段重命名就OK啦,但是这是线上数据库,再想直接F2改动是不可能的啦 ...

  9. Linux 中的键盘映射【转】

    本文转载自:http://hessian.cn/p/144.html [转]Linux 中的键盘映射 原文地址:http://www.linuxidc.com/Linux/2011-04/35197. ...

  10. 95.Extjs 表单中自定义的验证规则 VTypes

    1 Ext.onReady(function(){ Ext.QuickTips.init(); //重写 (自定义)xtype Ext.apply(Ext.form.VTypes,{ repetiti ...