Go: package main

import (
	"bufio"
			"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		m, _ := strconv.Atoi(scanner.Text())
		if m == 0 {
			break
		}
		ab := make([][]int, 0)
		for i := 0; i < m; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			buf := make([]int, len(input))
			for j := range buf {
				buf[j], _ = strconv.Atoi(input[j])
			}
			ab = append(ab, buf)
		}
		dp := make([][]int, 8)
		for i := range dp {
			dp[i] = make([]int, 1001)
		}
		dp[0][0] = 1
		for i := 1; i < m+1; i++ {
			for j := 0; j < 1001; j++ {
				if dp[i-1][j] == 0 {
					continue
				}
				s := j
				for k := ab[i-1][1]; k > -1; k-- {
					if s < 1001 {
						dp[i][s] += dp[i-1][j]
					}
					s += ab[i-1][0]
				}
			}
		}
		scanner.Scan()
		g, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < g; i++ {
			scanner.Scan()
			n, _ := strconv.Atoi(scanner.Text())
			fmt.Println(dp[m][n])
		}
	}
}

C++: #include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin >> n){
        if(n==0)
            break;
            
        int dp[2][10000] = {0};
        dp[0][0] = 1;
        dp[1][0] = 1;
        for(int i=0;i<n;i++){
            int a,b;
            cin >> a >> b;
            for(int k=0;k<=1000;k++){
                if(dp[0][k]!=0){
                    for(int i=1;i<=b;i++){
                        dp[1][k+a*i]+= dp[0][k];
                    }     
                }
            }
            for(int k=0;k<=1000;k++)
                dp[0][k] = dp[1][k];
        }
        int m;
        cin >> m;
        for(int j=0;j<m;j++){
            int k;
            cin >> k;
            cout << dp[0][k] << endl;
        }      
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define NMAX 1000

int main() {
  int m, g, n, s;
  int a[7], b[7], dp[2][NMAX+1];
  while (1) {
    scanf("%d", &m);
    if (!m) break;
    for (int i=0; i<m; i++) {
      scanf("%d %d", &a[i], &b[i]);
    }

    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    s = 0;
    for (int i=0; i<m; i++) {
      memcpy(dp[!(i&1)], dp[i&1], sizeof(dp[0]));
      for (int j=0; j<=s; j++) {
        if (!dp[i&1][j]) continue;
        for (int k=0,l=j+a[i]; k<b[i] && l<=NMAX; k++,l+=a[i]) {
          dp[!(i&1)][l] += dp[i&1][j];
        }
      }
      s += a[i]*b[i];
      s = min(s, NMAX);
    }

    scanf("%d", &g);
    for (int i=0; i<g; i++) {
      scanf("%d", &n);
      printf("%d\n", dp[m&1][n]);
    }
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int as[7];
int bs[7];
int m;
int sums[100 * 10 + 1];
void solve(int n, int sum) {
	if (sum > 1000)
		return;
	if (n == m) {
		sums[sum]++;
		return;
	}
	for (int i = 0; i <= bs[n]; i++) {
		solve(n + 1, sum + (i * as[n]));
	}
}

int main() {
	while (cin >> m, m) {
		memset(sums, 0, sizeof(sums));
		for (int i = 0; i < m; i++) {
			cin >> as[i];
			cin >> bs[i];
		}
		solve(0, 0);
		int g;
		cin >> g;
		for (int i = 0; i < g; i++) {
			int ni;
			cin >> ni;
			cout << sums[ni] << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

void solve(const vi& a,const vi& b,int p,int sum,vi& c)
{
	if(p==a.size())
		c[sum]++;
	else
		rep(i,b[p]+1)
			solve(a,b,p+1,sum+a[p]*i,c);
}

int main()
{
	for(int m;cin>>m,m;){
		vi a(m),b(m);
		rep(i,m)
			cin>>a[i]>>b[i];

		vi c(7001);
		solve(a,b,0,0,c);
		int g; cin>>g;
		rep(i,g){
			int n; cin>>n;
			cout<<c[n]<<endl;
		}
	}
	
	return 0;
}

C++: #include<iostream>
#include<map>
using namespace std;

int m,M,*w,*v,*c,g,*n,cnt;

void solve(int i, int n){
	if(n==0){
		cnt++;
		return;
	}
	if(i == M)
		return;
	else if(c[i] > n){
		solve(i+1, n);
	}else{
		solve(i+1, n-c[i]);
		int x;
		for(x=1; i+x<M; x++)
			if(c[i+x] != c[i])
				break;
		solve(i+x, n);
	}
}

int main(){
	while(true){
		M = 0;
		cin >> m;
		if(!m)
			return 0;
		w = new int[m];
		v = new int[m];
		for(int i=0; i<m; i++){
			cin >> w[i] >> v[i];
			M += v[i];
		}
		c = new int[M];
		cnt = 0;
		for(int i=0; i<m; i++){
			for(int k=1; k<=v[i]; k++){
				c[cnt] = w[i];
				cnt++;
			}
		}
		cnt = 0;
		cin >> g;
		n = new int[g];
		for(int i=0; i<g; i++)
			cin >> n[i];
		for(int i=0; i<g; i++){
			cnt = 0;
			solve(0, n[i]);
			cout << cnt << endl;
		}
		delete[] w;
		delete[] v;
		delete[] c;
	}
}

C++: #include <cstdio>

int dp[8][1001],a[8],b[8],m,g,n;

int main(){
	while(scanf("%d",&m),m != 0){
		for(int i = 1; i <= m; i++){
			scanf("%d%d",&a[i],&b[i]);
		}
		for(int i = 0; i < 8; i++){
			for(int j = 0; j < 1001; j++){
				dp[i][j] = 0;
			}
		}
		dp[0][0] = 1;
		for(int i = 1; i <= m; i++){
			for(int j = 0; j < 1001; j++){
				if(dp[i-1][j] > 0){
					for(int k = 0; k <= b[i]; k++){
						if(j + k*a[i] <= 1000){
							dp[i][j+k*a[i]] += dp[i-1][j];
						}
					}
				}
			}
		}
				

		scanf("%d",&g);
		for(int i = 0; i < g; i++){
			scanf("%d",&n);
			printf("%d\n",dp[m][n]);
		}
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
int main() {
	while (1) {
		int M; cin >> M;
		if (!M)break;
		vector<pair<int, int>>cards;
		for (int i = 0; i < M; ++i) {

			int a, b; cin >> a >> b;
			cards.emplace_back(a, b);
		}
		vector<vector<int>>dp(M + 1, vector<int>(10000));
		dp[0][0] = 1;
		for (int i = 0; i < M; ++i) {
			for (int j = 0; j < 9000; ++j) {
				for (int k = 0; k <= cards[i].second; ++k) {
					int plus = dp[i][j];
					for (int l = 0; l < k; ++l) {
						plus *= cards[i].second-l;
					}
					for (int l = 0; l < k; ++l) {
						plus /= l + 1;
					}
					dp[i + 1][j + k*cards[i].first] +=dp[i][j];
				}
			}
		}
		int G; cin >> G;
		for (int i = 0; i < G; ++i) {
			int n; cin >> n;
			cout << dp[M][n] << endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
using namespace std;

int main()
{
	int N,num[8],s[8];
	while(scanf("%d", &N), N)
	{
		for(int i=1; i<=N; i++)
			scanf("%d%d", &num[i], &s[i]);
		
		int dp[1001]={0};
		dp[0]=1;
		
		for(int i=1; i<=N; i++)
		{
			bool c[1001]={0};
			c[0]=1;
			for(int j=0; j<s[i]; j++)
			for(int k=1000; k>=0; k--)
			{
				if(k+num[i]>1000) continue;
				if(c[k]!=0) c[k+num[i]]=1;
			}
			
			for(int k=1000; k>=0; k--)
			for(int j=1; j<=s[i]*num[i]; j++)
			{
				if(k+j>1000) continue;
				if(c[j]!=0) dp[k+j]+=dp[k];
			}
		}
		
		int M;
		scanf("%d", &M);
		while(M--)
		{
			int t;
			scanf("%d", &t);
			printf("%d\n", dp[t]);
		}
	}
}