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>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int m;
  while(cin >> m, m) {
    int card[m], num[m];
    REP(i,m)
      cin >> card[i] >> num[i];
    int dp[m+1][1001];
    memset(dp,0,sizeof(dp));

    dp[0][0] = 1;
    for(int i=1; i<=m; ++i) {
      REP(j,1001)
        dp[i][j] = dp[i-1][j];
      for(int k=1; k<=num[i-1]; ++k) {
        for (int j=0; j + k*card[i-1] < 1001; ++j) {
          if (dp[i-1][j]>0) {
            dp[i][j + k*card[i-1]] += dp[i-1][j] ;
            //    printf("%d,%d,%d %d\n",i,j,j + k*card[i-1],dp[i][j + k*card[i-1]]);
          }
        }
      }
    }
    int g;
    cin >> g;
    while(g--) {
      int a;
      cin >> a;
      cout << dp[m][a] << endl;
    }
  }
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;

int main(){
    int n;
    while(cin >> n,n){
        vector<pair<int, int>> v;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            v.emplace_back(a,b);
        }

        int dp[1005] = {0};
        dp[0] = 1;
        rep(i,n){
            for(int j = 1000; j >= 0; j--){
                range(k,1,v[i].second + 1){
                    if(j - v[i].first * k < 0) continue;
                    dp[j] += dp[j - v[i].first * k];
                }
            }
        }

        int g;
        cin >> g;
        rep(i,g){
            int k;
            cin >> k;
            cout << dp[k] << endl;
        }
    }
}

C++: #include<iostream>
#include<vector>
#include<cstring>

using namespace std;

typedef pair<int,int> P;
const int MAX = 1001;

int Ans[8][MAX];
vector<P> V;

void make(){
  for(int i = 0; i < V.size(); i++)
    for(int k = 0; k < MAX; k++)
      if(Ans[k])
	for(int j = 0; j <= V[i].second; j++){
	  if(k+V[i].first*j >= MAX)break;
	  Ans[i+1][k+V[i].first*j] += Ans[i][k];
	}
}

void init(){
  memset(Ans,0,sizeof(Ans));
  Ans[0][0] = 1;
  V.clear();
}

int main(){

  int m;
  while(cin >> m && m){
    init();
    for(int i = 0; i < m; i++){
      P p;
      cin >> p.first >> p.second;
      V.push_back(p);
    }

    make();

    int g;
    cin >> g;
    while(g--){
      int n;
      cin >> n;
      cout << Ans[V.size()][n] << endl;
    }

  }
  return 0;
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,b,n) for(int i=b;i<n;i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int m,a,b;
int g,n;
pii card[110];
int dp[100][1010];
int main(){
	while(cin>>m,m){
		memset(card,0,sizeof(card));
		memset(dp,0,sizeof(dp));
		rep(i,m){
			cin>>a>>b;
			card[i].first=a;
			card[i].second = b;
		}
		cin>>g;
		for(int j =0;j <=card[0].second&&card[0].first*j<=1000;j++){
			dp[0][card[0].first*j]++;
		}
		int j;
		for(int i =1;i <m;i++){
			j =card[i].first;
			for(int k =0;k <=card[i].second;k++){
				for(int l =0;l+j*k<=1000;l++){
					dp[i][l+j*k]+=dp[i-1][l];
					//cout <<dp[i][l+j*k]<<endl;
				}
			}
		}

		rep(i,g){
			cin>>n;
			cout <<dp[m-1][n]<<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

int exist[100000];

int main(){
	
	while(true){
		
		int m;
		cin >> m;
		if(m == 0){
			break;
		}
		
		for(int i = 0; i < 10000; i++){
			exist[i] = 0;
		}
		
		exist[0] = 1;
		
		int a, b;
		
		for(int loop = 0; loop < m; loop++){
			cin >> a >> b;
			for(int i = 9999; i >= 0; i--){
				if(exist[i] > 0){
					for(int j = 1; j <= b; j++){
						exist[i + a * j] += exist[i];
					}
				}
			}
		}
		
		int g;
		
		cin >> g;
		
		for(int loop = 0; loop < g; loop++){
			int n;
			cin >> n;
			cout << exist[n] << endl;
		}
		
	}
	return 0;
}

C++: #include <bits/stdc++.h>

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = init; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct
{
    int to;
    int cost;
};

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int dp[101][1001]; // i, sum

int main()
{
    int M;
    
    while(cin >> M, M)
    {
        vector<int> a(M), b(M);
        rep(i, M) cin >> a[i] >> b[i];

        memset(dp, 0, sizeof(dp));

        dp[0][0] = 1;

        rep(i, M)
        {
            rep(j, b[i] + 1)
            {
                rep(k, 1001)
                {
                    if(k + j * a[i] <= 1000) dp[i + 1][k + j * a[i]] += dp[i][k];
                }
            }
        }

        int G;
        cin >> G;

        rep(i, G)
        {
            int N;
            cin >> N;
            cout << dp[M][N] << endl; 
        }
    }

    return 0;
}