UVA - 12298 Super Poker II (FFT+母函数)
题意:有四种花色的牌,每种花色的牌中只能使用数值的约数个数大于2的牌.现在遗失了c张牌.每种花色选一张,求值在区间[a,b]的每个数值的选择方法有多少.
分析:约数个数大于2,即合数.所以先预处理出50000内的所有素数.
然后根据给出的c个遗失牌和素数与否.构造生成多项式,因为上限是b,所以每个多项式只需构造b项即可.4类牌对应4个多项式,求三次卷积求出答案.
坑点:复数类里要用long double
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 4e5 + 10;
const long double PI = acos(-1.0);
struct Complex{
long double x, y;
inline Complex operator+(const Complex b) const {
return (Complex){x +b.x,y + b.y};
}
inline Complex operator-(const Complex b) const {
return (Complex){x -b.x,y - b.y};
}
inline Complex operator*(const Complex b) const {
return (Complex){x *b.x -y * b.y,x * b.y + y * b.x};
}
} va[MAXN * 2 + MAXN / 2], vb[MAXN * 2 + MAXN / 2];
int lenth = 1, rev[MAXN * 2 + MAXN / 2];
int N, M; // f 和 g 的数量
//f g和 的系数
// 卷积结果
// 大数乘积
int f[MAXN],g[MAXN];
vector<LL> conv;
vector<LL> multi;
//f g
void init()
{
int tim = 0;
lenth = 1;
conv.clear(), multi.clear();
memset(va, 0, sizeof va);
memset(vb, 0, sizeof vb);
while (lenth <= N + M - 2)
lenth <<= 1, tim++;
for (int i = 0; i < lenth; i++)
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (tim - 1));
}
void FFT(Complex *A, const int fla)
{
for (int i = 0; i < lenth; i++){
if (i < rev[i]){
swap(A[i], A[rev[i]]);
}
}
for (int i = 1; i < lenth; i <<= 1){
const Complex w = (Complex){cos(PI / i), fla * sin(PI / i)};
for (int j = 0; j < lenth; j += (i << 1)){
Complex K = (Complex){1, 0};
for (int k = 0; k < i; k++, K = K * w){
const Complex x = A[j + k], y = K * A[j + k + i];
A[j + k] = x + y;
A[j + k + i] = x - y;
}
}
}
}
void getConv(){ //求多项式
init();
for (int i = 0; i < N; i++)
va[i].x = f[i];
for (int i = 0; i < M; i++)
vb[i].x = g[i];
FFT(va, 1), FFT(vb, 1);
for (int i = 0; i < lenth; i++)
va[i] = va[i] * vb[i];
FFT(va, -1);
for (int i = 0; i <= N + M - 2; i++)
conv.push_back((LL)(va[i].x / lenth + 0.5));
}
void getMulti() //求A*B
{
getConv();
multi = conv;
reverse(multi.begin(), multi.end());
multi.push_back(0);
int sz = multi.size();
for (int i = 0; i < sz - 1; i++){
multi[i + 1] += multi[i] / 10;
multi[i] %= 10;
}
while (!multi.back() && multi.size() > 1)
multi.pop_back();
reverse(multi.begin(), multi.end());
}
const int up = 500005;
bool check[up];
int cnt[up];
void pre()
{
for(int i=2;i<up;++i){
if(check[i]) continue;
for(int j=2*i;j<up;j+=i){
check[j] = true;
}
}
}
bool lack[4][up];
char str[100];
int main()
{
pre();
int a,b,c;
while(scanf("%d %d %d",&a, &b, &c)==3){
if(!a && !b && !c) continue;
memset(lack,0,sizeof(lack));
for(int i=1;i<=c;++i){
int tmp = 0; scanf("%d",&tmp);
char c; scanf("%c",&c);
if(c=='S') lack[0][tmp] = true;
else if(c=='H') lack[1][tmp] = true;
else if(c=='C') lack[2][tmp] = true;
else lack[3][tmp] = true;
}
N = b;
for(int i=0;i<b;++i){
if(check[i] && !lack[0][i]) f[i] = 1;
else f[i] =0;
}
for(int i=1;i<4;++i){
if(i>1){
N = conv.size();
for(int j=0;j<N;++j){
f[j] = conv[j];
}
}
M = b;
for(int j=1;j<b;++j){
if(check[j] && !lack[i][j]) g[j] = 1;
else g[j] = 0;
}
getConv();
}
for(int i=a;i<=b;++i){
printf("%lld\n",conv[i]);
}
puts("");
}
return 0;
}
UVA - 12298 Super Poker II (FFT+母函数)的更多相关文章
- UVA 12298 Super Poker II (FFT)
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using ...
- UVA - 12298 Super Poker II NTT
UVA - 12298 Super Poker II NTT 链接 Vjudge 思路 暴力开个桶,然后统计,不过会T,用ntt或者fft,ntt用个大模数就行了,百度搜索"NTT大模数&q ...
- UVa12298 Super Poker II(母函数 + FFT)
题目 Source http://acm.hust.edu.cn/vjudge/problem/23590 Description I have a set of super poker cards, ...
- FFT(快速傅里叶变换):UVAoj 12298 - Super Poker II
题目:就是现在有一堆扑克里面的牌有无数张, 每种合数的牌有4中不同花色各一张(0, 1都不是合数), 没有质数或者大小是0或者1的牌现在这堆牌中缺失了其中的 c 张牌, 告诉你a, b, c接下来c张 ...
- Super Poker II UVA - 12298 FFT_生成函数
Code: #include<bits/stdc++.h> #define maxn 1000000 #define ll long long #define double long do ...
- bzoj2487: Super Poker II
Description I have a set of super poker cards, consisting of an infinite number of cards. For each p ...
- UVA12298 Super Poker II
怎么又是没人写题解的UVA好题,个人感觉应该是生成函数的大板子题了. 直接做肯定爆炸,考虑来一发优化,我们记一个多项式,其中\(i\)次项的系数就表示对于\(i\)这个数有多少种表示方式. 那么很明显 ...
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
- UVA 10869 - Brownie Points II(树阵)
UVA 10869 - Brownie Points II 题目链接 题意:平面上n个点,两个人,第一个人先选一条经过点的垂直x轴的线.然后还有一个人在这条线上穿过的点选一点作垂直该直线的线,然后划分 ...
随机推荐
- 提高ASP.NET网站性能的方法
http://www.360doc.com/content/14/0705/18/7662927_392224856.shtml Asp.NET有许多秘密,当你了解了这些秘密后,可以使得你的ASP ...
- 让iOS应用支持不同版本的系统与设备
本文转载至 http://blog.csdn.net/pucker/article/details/11980811 最近一直在做app的iOS 6和7的同时适配工作,所以在此介绍一下系统与设备的兼 ...
- __construct __destory __call __get __set
1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用. 我们知道 php5对象模型 < ,所以__construct()作为类的默认的构造函数 而不会调用同类名函 ...
- 透过Nim游戏浅谈博弈
452. Nim游戏! ★ 输入文件:nim!.in 输出文件:nim!.out 简单对比时间限制:1 s 内存限制:128 MB 甲,乙两个人玩Nim取石子游戏. nim游戏的规则是 ...
- [NOIP2017]列队 离线+SBT
[NOIP2017]列队 题目描述 Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵 ...
- 【BZOJ3238】[Ahoi2013]差异 后缀数组+单调栈
[BZOJ3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...
- datatables如何把列设置成hidden隐藏域?
官网:https://datatables.net/reference/option/设置: visible: false如下: <!DOCTYPE html><html>&l ...
- linux shell 脚本使用
定义变量 fileName=text.txt 变量名称fileName,变量名称text.txt 使用变量 $fileName 用美元符号$开头,后面加变量名称,即可使用变量 使用用户输入参数 打印第 ...
- Python全栈day18(迭代器协议和for循环工作机制)
一,什么是迭代和递归 递归和迭代都是循环的一种. 简单地说,递归是重复调用函数自身实现循环.迭代是函数内某段代码实现循环,而迭代与普通循环的区别是:循环代码中参与运算的变量同时是保存结果的变量,当前保 ...
- ubuntu重启不清除 /tmp 设置
gedit /etc/default/rcS, 把TMPTIME=0 修改成 TMPTIME=-1,保存退出即可.