2019.01.02 poj3046 Ant Counting(生成函数+dp)

版权声明:随意转载哦......但还是请注明出处吧: https://blog.csdn.net/dreaming__ldx/article/details/85612197

传送门
生成函数基础题。
题意:给出 n n 个数以及它们的数量,求从所有数中选出 i i [ L , R ] i|i\in[L,R] 个数来可能组成的集合的数量。


直接构造生成函数然后乘起来 f ( x ) = i = 1 n ( 1 + x + x 2 + . . . + x t i m e i ) f(x)=\prod_{i=1}^n(1+x+x^2+...+x^{time_i}) 然后求出系数即可。
由于模数是 1 e 6 1e6 无法 n t t ntt ,考虑到数据很小可以直接用 d p dp 来转移(分组背包)
代码:

#include<iostream>
#include<cctype>
#include<cstdio>
#define ri register int
using namespace std;
const int mod=1e6;
#define add(a,b) ((a)+(b)>=mod?(a)+(b)-mod:(a)+(b))
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
int ans=0,T,a,L,R,cnt[1005],f[100005];
int main(){
	T=read(),a=read(),L=read(),R=read(),f[0]=1;
	for(ri i=1;i<=a;++i)++cnt[read()];
	for(ri i=1;i<=T;++i)for(ri k=R;~k;--k)for(ri j=min(cnt[i],k);j;--j)f[k]=add(f[k-j],f[k]);
	for(ri i=L;i<=R;++i)ans=add(ans,f[i]);
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/dreaming__ldx/article/details/85612197