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.

我有一副超级扑克包含无数张牌,如果数字是正合数的话,那就有四种牌:黑桃(S)、红桃(H)、梅花(C)、方片(D)。

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

比如6有4个因子(1,2,3,6)所以它是合数;7只有两个(1,7)那就不是合数。

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:

给定一个整数n,从四种花色各选一张牌,使得四张牌点数和为n,求解有多少种选法组合。比如n=24时,其中的一种选法可以为24=4+6+4+10,如下图所示:

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.

不幸的是有的牌丢失了,样例会给出哪些丢了。并且为了题目更加因吹斯听,实际上不是给你n,而是给两个数a、b,输出所有n=a、n=a+1、n=a+2、……、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.

输入有多组数据,每组数据第一行三个整数a、b、c,a、b如上,c为丢失卡牌数;第二行给出c个具体的卡牌为丢失的。abc均为0时输入结束。

Output
For each test case, print p integers, one in each line. 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

拜早年啦!年夜是刷题,还是边看番边刷题,还是带亲戚家小孩刷题啊?

口胡思路:

先以只有两色四张牌的简单题为例。假如第一个花色S有4和6两个点数,则可以用一个多项式表示所有可选情况:x^4 + x^6;第二个花色H有8和10两个点数,可用:x^8 + x^10表示。

我们将这两式相乘得(x^4 + x^6)(x^8 + x^10) = x^12 + 2 * x^14 + x^16,这个结果代表的意义就是:和为12的组合有1种,和为14的组合有2种(4S+10H或6S+8H),和为16的有1种。

好,那么对应到最开始的这道题目上来,就是稍微复杂了一点。对于每个花色,x的次幂为质数时系数为0,即多项式中只存在合数项。然后合数中也会有丢失的,其系数也置零。接下来四个多项式相乘即可,最终的多项式中x的次幂为n的项的系数,就是和为n的组合数。

事实上,这是离散数学中的一个小知识点,如题,是生成函数的一个简单应用,在此处帮大家复习一下。

另外,两多项式相乘的朴素做法显然是O(n^2)的,用FFT或者NTT加速成nlogn喽。不过快速傅里叶变换和数论变换不是一两句话可以口胡明白的,也不是本文想讲的知识,姑且略去……

最后,如果自己手写的FFT而没用complex库里的复数运算的话,这题卡精度,要用long double。

诸君来看代码吧:

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int maxn = ;
const long double PI = acos(-1.0); struct Complex {
long double x, y;
Complex(long double a = , long double b = ):x(a), y(b){}
};
//手写复数运算
Complex operator + (Complex A, Complex B) { return Complex(A.x + B.x, A.y + B.y); }
Complex operator - (Complex A, Complex B) { return Complex(A.x - B.x, A.y - B.y); }
Complex operator * (Complex A, Complex B) { return Complex(A.x * B.x - A.y * B.y, A.x * B.y + A.y * B.x); } Complex n[maxn * ], m[maxn * ];//像线段树一样要开4倍
int r[maxn * ], l, limit, nl;
/*---以上为FFT所需部分---*/ int a, b, c;//abc含义见题面
bool composite[maxn];//合数记录
bool lost[][maxn];//卡牌丢失记录 void init(int n) {//求50000以内的合数
int m = int(sqrt(n) + 0.5);
for (int i = ; i <= m; i++)
if (!composite[i])
for (int j = i * i; j <= n; j += i)
composite[j] = ;
} const char *ss = "SHCD";
int idx(char c) {//巧得丢失字符位置
return strchr(ss, c) - ss;
} /*--以下为一个普通的FFT板子--*/
void fft_init() {
limit = , l = ;
while (limit < *(b + )) {
limit <<= ;
l++;
} for (int i = ; i < limit; i++)
r[i] = (r[i>>] >> ) | ((i&) << (l-));
} void fft(Complex *A, int type) {
for (int i = ; i < limit; i++)
if (i < r[i])
swap(A[i], A[r[i]]); for (int mid = ; mid < limit; mid <<= ) {
Complex Wn(cos(PI / mid), type * sin(PI / mid));
for (int R = mid << , j = ; j < limit; j += R) {
Complex w(, );
for (int k = ; k < mid; k++, w = w * Wn) {
Complex x = A[j+k], y = w * A[j+k+mid];
A[j+k] = x + y;
A[j+k+mid] = x - y;
}
}
}
} void Fourier(Complex *A, Complex *B) {
fft(A, );
fft(B, );
for (int i = ; i < limit; i++)
A[i] = A[i] * B[i];
fft(A, -);
for (int i = ; i < nl + b + ; i++)
A[i].x /= limit, A[i].y = 0.0;
}
/*---FFT结束---*/ int main() {
init();//求5万以内合数
while (~scanf("%d%d%d", &a, &b, &c) && a) {
memset(lost, false, sizeof(lost));//丢牌数组
for (int i = ; i < c; i++) {
int temp; char ch;
scanf("%d%c", &temp, &ch);
lost[idx(ch)][temp] = ;//这张牌丢了
} n[].x = 1.0, n[].y = 0.0;//同下方m数组注释
nl = ;
fft_init();//这个东西反复调用没意义就先拿出来了
for (int i = ; i < ; i++) {
//n是上一轮结束时的多项式结果,nl是多项式n的长度
//答案只要求a到b的多项式系数,所以次幂为b以上可以置零
for (int j = nl; j <= limit; j++)
n[j].x = n[j].y = 0.0;
//m数组用来表示当前花色的多项式,所以先全置零
for (int j = ; j <= limit; j++)
m[j].x = m[j].y = 0.0; for (int j = ; j <= b; j++) {
if (composite[j] && !lost[i][j])
m[j].x = 1.0, m[j].y = 0.0;
//将这个合数的多项式系数设为复数(1 + 0*i),即实数的1
}
Fourier(n, m);//n和m这两个多项式相乘
nl = b + ;//b+1的原因是常数项也是一项
} for (int i = a; i <= b; i++)//x为实数项,即系数
printf("%lld\n", (long long)(n[i].x + 0.5));
puts("");
}
return ;
}

UVa12298(生成函数的简单应用+FFT)的更多相关文章

  1. uva12298(生成函数)

    生成函数的一般应用: #include<iostream> #include<cstring> #include<cmath> #include<cstdio ...

  2. 简单的 FFT 变形 - BZOJ 2194

    「BZOJ2194」快速傅立叶之二 2015年4月29日3,8300 Description 请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 ...

  3. FFT的物理意义

    来源:学步园 FFT(Fast Fourier Transform,快速傅立叶变换)是离散傅立叶变换的快速算法,也是我们在数字信号处理技术中经常会提到的一个概念.在大学的理工科课程中,在完成高等数学的 ...

  4. BZOJ.4909.[SDOI2017]龙与地下城(正态分布 中心极限定理 FFT Simpson积分)

    BZOJ 洛谷 https://www.luogu.org/blog/ShadowassIIXVIIIIV/solution-p3779# 正态分布 正态分布是随机变量\(X\)的一种概率分布形式.它 ...

  5. 2016 MIPT Pre-Finals Workshop Taiwan NTU Contest

    2016弱校联盟十一专场10.5 传送门 A. As Easy As Possible 假设固定左端点,那么每次都是贪心的匹配\(easy\)这个单词. 从\(l\)开始匹配的单词,将\(y\)的位置 ...

  6. bzoj2487: Super Poker II

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

  7. 『TensorFlow』SSD源码学习_其四:数据介绍及TFR文件生成

    Fork版本项目地址:SSD 一.数据格式介绍 数据文件夹命名为VOC2012,内部有5个子文件夹,如下, 我们的检测任务中使用JPEGImages文件夹和Annotations文件夹. JPEGIm ...

  8. [USACO19DEC]Tree Depth P

    题意 求逆序对为\(k\)的\(n\)排列中,生成的笛卡尔数,每个位置的深度和.\(n\le 300\) 做法 设\(f_{k}\)为\(n\)排列中逆序对为\(k\)的个数,其生成函数为:\[\pr ...

  9. bzoj 3513 [MUTC2013]idiots FFT 生成函数

    [MUTC2013]idiots Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 806  Solved: 265[Submit][Status][Di ...

随机推荐

  1. CSS动画硬件加速

    http://zencode.in/14.CSS%E5%8A%A8%E7%94%BB%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html http:// ...

  2. sdut oj 排队买饭

    数据结构实验之队列一:排队买饭 Time Limit: 1000MS Memory limit: 65536K 题目描述 中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接 ...

  3. hadoop 常用命令总结

    1. 查看集群资源信息 hdfs dfsadmin -report 2. 启动一个mapreduce任务, hadoop jar /opt/hadoop/share/hadoop/tools/lib/ ...

  4. php排序方法之选择排序

    //选择排序法 $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function selectSort($arr){ for ( $i=0; $i<cou ...

  5. js 分享代码--完整示例代码

    <div class="bdsharebuttonbox" data-tag="share_1"> <a class="bds_ms ...

  6. Rsync+Inotify同步

    rsync服务安装与<rsync+sersync同步>环境一样! 安装inotify-tools 在源服务器10.10.2.191上操作: 1.查看服务器内核是否支持inotify ll ...

  7. 使用 StoryBoard 制作一个能够删除cell的TableView

    本篇博客方便自己检索使用.资源链接 下面是制作效果图,点击删除按钮,就能将该cell删除: 下面是主要的代码: #define KSUPER_TAG 20000 #define KDEFAU_TAG ...

  8. Bayesian 网络分类算法

    1:贝叶斯网络的定义和性质 一个贝叶斯网络定义包括一个有向无环图(DAG)和一个条件概率表集合.DAG中每一个节点表示一个随机变量,可以是可直接观测变量或隐藏变量,而有向边表示随机变量间的条件依赖:条 ...

  9. virtualBox中的centOS虚拟机硬盘扩容

    1. 在virtualBox中给虚拟机添加虚拟硬盘 此时. 已经将yanwu_disk1.vdi 虚拟硬盘添加到了虚拟机中, 接下来就是进行硬盘的挂载 https://www.cnblogs.com/ ...

  10. spark学习之IDEA配置spark并wordcount提交集群

    这篇文章包括以下内容 (1)IDEA中scala的安装 (2)hdfs简单的使用,没有写它的部署 (3) 使用scala编写简单的wordcount,输入文件和输出文件使用参数传递 (4)IDEA打包 ...