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. 【ACM】nyoj_14_会场安排问题_201308151955

    会场安排问题时间限制:3000 ms  |  内存限制:65535 KB 难度:4描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工作就是安 ...

  2. .net performance optimize your C# app 读书笔记

    目录 序 作者简介 推荐人简介 感谢 本书简介 第一章  性能指标 第二章  性能测量 第三章  内部类型 第四章  垃圾回收机制 第五章  集合和泛型 第六章  并发和并行性 第七章  网络.I / ...

  3. ubuntu 配置静态路由

    原文:http://blog.sina.com.cn/s/blog_6fd8d5d90101f1xy.html -------------------------------------------- ...

  4. 全部对于Unity3D中 NGUI 触发事件的监听方法

    NGUI事件的种类非常多.比方点击.双击.拖动.滑动等等,他们处理事件的原理差点儿万全一样,本文仅仅用button来举例. 方法一.直接监听事件 把以下脚本直接绑定在button上.当button点击 ...

  5. JS0基础学习笔记(1)

    为了须要,最近開始学习JS相关知识.基本的方式是通过看视频以及查阅相关手冊.并动手实践,亲手写出每一个小案例,以下是相关代码(每一个案例用分隔线隔开). <!DOCTYPE html> & ...

  6. 《黑马程序猿》 cocos2d游戏引擎复习笔记一

    /** ----------------------------游戏场景的搭建-------------------------------- 1首先创建一个surfaceview ,它能够在子线程中 ...

  7. Delphi7中单元文件内各个部分的执行顺序

    注:本文主要是讨论delphi程序启动时和退出时的执行顺序,期间有些知识来源于Delphi帮助,有些来自<Delphi7程序设计教程>(这本书只告诉我有initialization 和 f ...

  8. P1043 数字游戏

    P1043 数字游戏 题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n ...

  9. Spring:验证用户登录

    利用 Spring IOC 技术实现用户登录的验证机制,对用户进行登录验证. 首先利用 Spring 的自动装配模式将 User 对象注入到控制器中,然后将用户输入的用户名和密码与系统中限定的合法用户 ...

  10. B1192 [HNOI2006]超级英雄Hero 二分图匹配

    先检讨一下,前一段时间开学,做题懒得发博客,也不总结...现在捡起来. 这个题一看是裸的二分图匹配,但是仔细一看还有一些区别,就是必须要连续的连接,否则直接退出.因为前一道题答不出来的话后面的题就没有 ...