貌似跟1171一样的,呃。。。
My Code:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 100;
struct node {
int val;
int num;
} a[55];
int c1[N], c2[N];
int main() {
//freopen("data.in", "r", stdin);
int t, n, i, j, sum, k, m;
while(cin >> t) {
while(t--) {
scanf("%d%d", &n, &m);
for(i = 1; i <= m; ++i) {
scanf("%d%d", &a[i].val, &a[i].num);
}
for(i = 0; i <= n; i++) {
c1[i] = 1; c2[i] = 0;
}
sum = a[1].val * a[1].num;
for(i = 2; i <= m; ++ i) {
for(j = 0; j <= n && j <= sum; ++ j) {
for(k = 0; k + j <= n && k <= a[i].val * a[i].num; k += a[i].val) {
c2[k+j] += c1[j];
}
}
sum += a[i].val * a[i].num;
for(j = 0; j <= sum && j <= n; ++ j) {
c1[j] = c2[j]; c2[j] = 0;
}
}
printf("%d\n", c1[n]);
}
}
return 0;
}