先考虑只有一个 的情况。
设 表示总和为 的进食序列数,那么有一个朴素的转移:。
其中 表示 的值域, 表示消耗 份饲料的动物数。
很大,但这个转移比较简单,并且 也很小,考虑矩阵加速:
转移矩阵 的大小为 。其形式为:
然后我们就可以 转移了,答案就是 ,但这还不够,因为我们有 个 。
但我们发现一个只有一行的矩阵乘转移矩阵是 的,并且乘完之后的结果还是一行的矩阵,所以我们预处理一下 的 次方( 最大取到 就行),每一次询问的时候用 凑出 ,一开始用 直接去乘就行了,总时间复杂度 。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=105,Mat=100,mod=1e9+7;
int n,m;
int a[N],mp[N],cnt[N];
int f[N];
struct node{
int a[N][N];
node (){memset(a,0,sizeof a);}
int* operator[](int x){return a[x];}
void getI(){
for(int i=1;i<=Mat;i++) a[i][i]=1;
}
node operator*(const node &x)const{
node c;
for(int i=1;i<=Mat;i++){
for(int j=1;j<=Mat;j++){
for(int k=1;k<=Mat;k++) c[i][j]=(c[i][j]+a[i][k]*x.a[k][j])%mod;
}
}
return c;
}
}A,dp,M[65];
node mults(node x,node y){
node res;
for(int i=1;i<=Mat;i++){
for(int j=1;j<=Mat;j++){
res[1][i]=(res[1][i]+x[1][j]*y[j][i])%mod;
}
}
return res;
}
node ksm(node x,int y){
node res;
res.getI();
while(y){
if(y&1) res=res*x;
y>>=1,x=x*x;
}
return res;
}
signed main(){
int maxn=0;
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]),maxn=max(maxn,a[i]),mp[a[i]]++;
for(int i=1;i<=maxn;i++) cnt[i]=mp[i];
f[0]=1;
for(int i=1;i<=maxn;i++){
for(int j=1;j<=maxn;j++) if(i>=j) f[i]+=(cnt[j]*f[i-j])%mod;
}
dp[1][1]=1;
for(int i=1;i<=maxn;i++) A[1][i]=cnt[i];
for(int i=2;i<=maxn;i++) A[i][i-1]=1;
M[0]=A;
for(int i=1;i<=64;i++) M[i]=M[i-1]*M[i-1];
while(m--){
int x;
scanf("%lld",&x);
if(x<=maxn) printf("%lld\n",f[x]);
else{
node res=dp;
for(int i=0;i<=60;i++) if(x&(1ll<<i)) res=mults(res,M[i]);
printf("%lld\n",res[1][1]);
}
}
return 0;
}