-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivisibleGroupSums_UVA10616.cpp
74 lines (57 loc) · 1.6 KB
/
divisibleGroupSums_UVA10616.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
ifstream fin("divisibleGroupSums_UVA10616.in");
ofstream fout("divisibleGroupSums_UVA10616.out");
int main()
{
int t = 0;
while (true)
{
++t;
int n = 0, q = 0; fin >> n >> q;
if (n + q == 0) break;
vector<long long> in(n);
for (int i = 0; i <= n - 1; ++i)
{
fin >> in[i];
}
fout << "SET " << t << ":\n";
for (int qi = 1; qi <= q; ++qi)
{
int m, d; fin >> d >> m;
long long _max = 0;
vector<long long> a = in;
for (int i = 0; i <= n - 1; ++i)
{
a[i] %= d;
if (a[i] < 0)
{
a[i] += d;
}
_max += a[i];
}
vector<vector<long long>> dp(n + 2, vector<long long>(_max * 2 + 1, 0));
dp[0][0] = 1;
for (int i = 0; i <= n - 1; ++i)
{
for (int c = n; c >= 0; --c)
{
for (int s = 0; s <= _max; ++s)
{
dp[c + 1][s + a[i]] += dp[c][s];
}
}
}
long long ans = 0;
if (m <= n)
{
for (int s = 0; s <= _max; s += d)
{
ans += dp[m][s];
}
}
fout << "QUERY " << qi << ": " << ans << '\n';
}
}
return 0;
}