这个题的意思是说给你t种蚂蚁a只,然后要求你在其中选出s,s+1,.....b 只共有多少种方法。结果只需要输出最后6位即可。
我没有用数学方法去做,最后只想到了动态规划的方法。转移方程是: dp[i][j]=(dp[i][j]+dp[i-1][j-k])%mod 表示前i种组成j的集合共有多少种。题目所述数组开不下,只有用滚动数组。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1100;
const int maxm=110000;
const int mod=1e6;
int t,a,s,b,cnt[maxn],num[maxn],type,dp[2][maxm];
int main()
{
while(scanf("%d%d%d%d",&t,&a,&s,&b)!=EOF)
{
memset(cnt,0,sizeof(cnt));
memset(dp,0,sizeof(dp));
type=0;
for(int i=0;i<a;i++)
{
int val;
scanf("%d",&val);
if(!cnt[val])
num[type++]=val;
cnt[val]++;
}
for(int i=0;i<=cnt[num[0]];i++)
dp[0][i]=1;
int sum=cnt[num[0]]+cnt[num[1]],pos=1;
for(int i=1;i<t;i++,sum+=cnt[num[i]])
{
dp[pos][0]=1;
for(int j=1;j<=sum;j++)
for(int k=0;k<=min(j,cnt[num[i]]);k++)
dp[pos][j]=(dp[pos][j]+dp[pos^1][j-k])%mod;
pos^=1;
memset(dp[pos],0,sizeof(dp[pos]));
}
int ans=0;
for(int i=s;i<=b;i++)
ans=(ans+dp[pos^1][i])%mod;
printf("%d\n",ans);
}
return 0;
}