#include <iostream>
#include <vector>
using namespace std;
int kn(vector<vector<vector<vector<int> > > > &dp,vector<int> &a,int m,int d,int i,int n,int sum){
// cout<<"sum: "<<sum<<"m: "<<m<<endl;
if(m==0){
if((sum%d)==0)return 1;
else return 0;
}
if(i>n)return 0;
if(dp[i][20+sum][d][m]!=-1)return dp[i][20+sum][d][m];
return dp[i][20+sum][d][m]=kn(dp,a,m,d,i+1,n,sum)+kn(dp,a,m-1,d,i+1,n,(sum+a[i])%d);
}
int main(){
int n,q,s=0;
while(cin>>n>>q){
s++;
if(n==0&&q==0)return 0;
vector<int> a(n+1);
int i=1;
while(i<=n)cin>>a[i++];
vector<vector<vector<vector<int> > > > dp(n+1,vector<vector<vector<int> > >(40+1,vector<vector<int> > (20+1,vector<int>(10+1,-1))));
i=1;
cout<<"SET "<<s<<":"<<endl;
while(i<=q){
int d,m;
cin>>d>>m;
cout<<"QUERY "<<i++<<": "<<kn(dp,a,m,d,1,n,0)<<endl;
}
}
return 0;
}
#include <vector>
using namespace std;
int kn(vector<vector<vector<vector<int> > > > &dp,vector<int> &a,int m,int d,int i,int n,int sum){
// cout<<"sum: "<<sum<<"m: "<<m<<endl;
if(m==0){
if((sum%d)==0)return 1;
else return 0;
}
if(i>n)return 0;
if(dp[i][20+sum][d][m]!=-1)return dp[i][20+sum][d][m];
return dp[i][20+sum][d][m]=kn(dp,a,m,d,i+1,n,sum)+kn(dp,a,m-1,d,i+1,n,(sum+a[i])%d);
}
int main(){
int n,q,s=0;
while(cin>>n>>q){
s++;
if(n==0&&q==0)return 0;
vector<int> a(n+1);
int i=1;
while(i<=n)cin>>a[i++];
vector<vector<vector<vector<int> > > > dp(n+1,vector<vector<vector<int> > >(40+1,vector<vector<int> > (20+1,vector<int>(10+1,-1))));
i=1;
cout<<"SET "<<s<<":"<<endl;
while(i<=q){
int d,m;
cin>>d>>m;
cout<<"QUERY "<<i++<<": "<<kn(dp,a,m,d,1,n,0)<<endl;
}
}
return 0;
}
Comments
Post a Comment