bzoj 3027 [Ceoi2004] Sweet —— 生成函数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3027
就是 (1+x+x2+...+xm[i]) 乘起来;
原来想和背包一样做,然而时限很短,数组也开不了很多,本来以为勉强一下也可以,后来突然发现不行...
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const xn=1e7+,mod=;
int n,a,b,s[xn],m;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
int upt(int x){while(x>=mod)x-=mod; while(x<)x+=mod; return x;}
int main()
{
n=rd(); a=rd(); b=rd(); m=rd();
for(int i=;i<=m;i++)s[i]=i+;
for(int i=m+;i<=b;i++)s[i]=s[m];//
for(int i=;i<=n;i++)
{
m=rd();
//mx=min(mx+m[i],b);
for(int j=b;j>=;j--)//
if(j-m->=)s[j]=upt(s[j]-s[j-m-]);
for(int j=;j<=b;j++)s[j]=upt(s[j]+s[j-]);
}
int ans=s[b];
if(a)ans=upt(ans-s[a-]);
printf("%d\n",ans);
return ;
}
TLE
首先,要化简这个多项式,得到 ∏(1-xm[i]+1) / (1-x)n
可以把分子和分母分开,分母就是熟悉的 ∑ C(n+i-1,n-1)*xi
而分子一共只有 n 项,可以 2n 搜出每个系数;
然后把二者组合在一起,对于搜出的 k * xy ,对答案有贡献还需要把 xy 变成 xa ~ xb ;
所以对应分母多项式的 xa-y ~ xb-y 的系数,是连续的组合数求和,杨辉三角里的一列;
但是模数不是质数,所以组合数不好算;
参考TJ,竟然可以对组合数和模数都乘 n!,就可以 O(n) 直接乘得到组合数了,最后把答案除以 n! 即可;
如果把搜到的系数存下来,最后遍历,复杂度反而成了 O(bn) ... 不如直接在搜索里计算,有值才算上,复杂度 O(n*2n)。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=,xm=1e7+,mod=;
int n,a,b,m[xn];
ll fac,p,ans;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
ll upt(ll x){while(x>=mod)x-=mod; while(x<)x+=mod; return x;}
ll C(int n,int m)
{
if(n<m)return ;//!
ll ret=;
for(int i=n-m+;i<=n;i++)ret=(ret*i)%p;
return ret;
}
void dfs(int x,int s,int t)
{
if(x==n+)
{
ans+=s*(C(n+b-t,n)-C(n+a-t-,n)); ans=ans%p;
return;
}
dfs(x+,s,t);
dfs(x+,-s,t+m[x]+);
}
int main()
{
n=rd(); a=rd(); b=rd(); fac=;
for(int i=;i<=n;i++)m[i]=rd(),fac*=i;
p=(ll)fac*mod;
dfs(,,);
/*
for(int y=0;y<=b;y++)
{
ll tmp=upt(C(n+b-y,n)-C(n+a-y-1,n));
ans=(ans+tmp*f[y])%p;
}
*/
if(ans<)ans+=p;
printf("%lld\n",ans/fac);
return ;
}
bzoj 3027 [Ceoi2004] Sweet —— 生成函数的更多相关文章
- bzoj 3027 [Ceoi2004]Sweet——生成函数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3027 化式子到 ( \mul_{i=1}^{n}(1-x^(m[i]+1)) ) / (1- ...
- bzoj 3027: [Ceoi2004]Sweet (生成函数)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3027. 题目大意:有$n$种数,每种有$C_i$个,问你在这些数中取出$[l,r]$个 ...
- bzoj 3027: [Ceoi2004]Sweet【生成函数+组合数学】
首先根据生成函数的套路,这个可以写成: \[ \prod_{i=1}^{n}(1+x^1+x^2+...+x^{c[i]}) \] 然后化简 \[ =\prod_{i=1}^{n}\frac{1-x^ ...
- BZOJ 3027: [Ceoi2004]Sweet
容斥 #include<cstdio> using namespace std; int a,b,n,m[15]; long long ans=0,mod=2004; long long ...
- 【BZOJ 3027】 3027: [Ceoi2004]Sweet (容斥原理+组合计数)
3027: [Ceoi2004]Sweet Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 71 Solved: 34 Description John ...
- [BZOJ3027][Ceoi2004]Sweet 容斥+组合数
3027: [Ceoi2004]Sweet Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 135 Solved: 66[Submit][Status] ...
- BZOJ3027 - [CEOI2004]Sweet
Portal Description 给出\(n(n\leq10),a,b(a,b\leq10^7)\)与\(\{c_n\}(c_i\leq10^6)\),求使得\(\sum_{i=1}^n x_i ...
- 2018.12.30 bzoj3027: [Ceoi2004]Sweet(生成函数+搜索)
传送门 生成函数好题. 题意简述:给出n个盒子,第iii个盒子里有mim_imi颗相同的糖(但不同盒子中的糖不相同),问有多少种选法可以从各盒子中选出数量在[a,b][a,b][a,b]之间的糖果. ...
- BZOJ 3027 Sweets 生成函数,容斥
Description John得到了n罐糖果.不同的糖果罐,糖果的种类不同(即同一个糖果罐里的糖果种类是相同的,不同的糖果罐里的糖果的种类是不同的).第i个糖果罐里有 mi个糖果.John决定吃掉一 ...
随机推荐
- Odoo超售订单
当 交付给客户的货物多于订购的数量时,就形成'超售'状态: 对于超售的部分,需要进行开票处理,以及根据情况修改交货 发生超售的前提是,产品开票策略为 '按订购数量开票' 同时需要 允许 ...
- Solaris主机间的信任关系机制
解决问题: 管理员经常在其他服务器之间登录,是否需要密码切换. 知识点:主机间信任关系.R 命令集 /etc/hosts/equiv 文件 R服务是不加密的,别人可以破解. 主机名 + 用户名. + ...
- php自定义错误
function myErrorHandler ( $errno , $errstr , $errfile , $errline ) { if (!( error_reporting () & ...
- soapUI学习笔记--用例字段参数化
字段参数化的简单操作 1.把Request新增一个TestCase 增加TestCase,下方会出现: 2.案例中,请求参数只有一个.先运行下请求,可以运行成功(保证接口是通的) 3.添加参数.见图中 ...
- Allegro封装的制作
过孔封装的层次分析: 1.阻焊层Solder Mask:又称绿油层,是PCB上的非布线层,用于制成丝网漏印板,将不需要的焊接的地方涂上阻焊剂.由于焊接PCB时焊锡在高温下的流动性,所以必须在不需要焊接 ...
- shell tr命令的使用
http://fyan.iteye.com/blog/1172279 tr是translate的简写,亦即翻译,但是遗憾的是,它不能翻译句子,只能翻译单个字符. 1 tr的工作原理是什么? 先记住一点 ...
- Spring源码学习(一)资源加载
这里先从最简单的一个Spring例子开始. 下面是Spring的context的配置 <?xml version="1.0" encoding="UTF-8&quo ...
- 一起学android之怎样卸载指定的 应用程序(25)
效果图例如以下: 代码例如以下: public class MainActivity extends Activity { private Button btn_delete; @Override p ...
- 【caffe-windows】 caffe-master 之图片转换成lmdb or leveldb
前期准备: 文件夹train:此文件夹中按类别分好子文件夹,各子文件夹里存放相应图片 文件夹test:同train,有多少类就有多少个子文件夹 trainlabels.txt : 存的是训练集的标签 ...
- CF 445A(DZY Loves Chessboard-BW填充)
A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...