1. 程式人生 > >2019.01.02 poj3046 Ant Counting(生成函式+dp)

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

傳送門
生成函式基礎題。
題意:給出 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;
}