-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubset_sum.cpp
More file actions
48 lines (48 loc) · 1.06 KB
/
Copy pathSubset_sum.cpp
File metadata and controls
48 lines (48 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<vector>
#include<fstream>
#include<map>
using namespace std;
typedef vector<int> vi;
int a[210];
long long int memo[210][210][25];
int n, m, d, q, s=1;
long long int value(int id,int p,long long int sum)
{
if(p==m && sum==0)return 1;
if(id==n)return 0;
if(memo[id][p][sum]!=-1)return memo[id][p][sum];
long long int tmp=0;
tmp+=value(id+1, p, sum);
tmp+=value(id+1, p+1, ((long long int)(((sum+a[id])%d)+d)%d));
memo[id][p][sum]=tmp;
return tmp;
}
int main()
{
while(1)
{
scanf("%d %d", &n, &q);
if(n==0 && q==0)break;
for(int i=0;i<n;i++)
scanf("%d", &a[i]);
int qq=1;
printf("SET %d:\n", s);
++s;
while(q--)
{
scanf("%d %d", &d, &m);
memset(memo, -1, sizeof memo);
long long int ans=value(0, 0, 0);
printf("QUERY %d: %lld\n", qq, ans);
++qq;
}
}
return 0;
}