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 a[7][2],c;
int f(int tc,int tp,int maxc,int maxp){
	if(tc == maxc) {
		if(maxp == tp) ++c;
	}
	else {
		for(int i = 0;i <= a[tc][1];++i){
			f(tc + 1,tp + (a[tc][0] * i),maxc,maxp);
		}
	}
	return 0;
}
int main(){
	int n,b,price;
	while(cin >> n,n){
		for(int i = 0;i < n;++i){
			cin >> a[i][0] >> a[i][1];
		}
		cin >> b;
		for(int i = 0;i < b;++i){
			c = 0;
			cin >> price;
			f(0,0,n,price);
			cout << c << endl;
		}
	}
	return 0;
}

C++: #include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define PREP(it,array) for(vector<P>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<int,int> P;





int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int m;
  while( cin>>m, m ){
    int a[7],b[7];
    REP(i,m){
      int aa,bb;
      cin >>aa>>bb;
      a[i] = aa;
      b[i] = bb;
    }

    int dp[7+1][1000+1];
    memset( dp, 0, sizeof(dp) );
    dp[0][0] = 1;

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

    int g;
    cin >> g;
    REP(_t,g){
      int n;
      cin >> n;
      cout << dp[m][n] << endl;
    }


  }

  return 0;
}

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

#define MAX_N 150000
#define MAX_CARD 120

long long dp[MAX_CARD][MAX_N];
long long card[MAX_CARD][2];
int n, m, a, b, cnt, sum;

int main() {
	while (true) {
		memset(dp, 0, sizeof(dp));
		memset(card, 0, sizeof(card));
		//cin.
		cin >> n;
		if (n == 0) {
			break;
		}
		cnt = 0;
		sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> a >> b;
			card[i][0] = a;
			card[i][1] = b;
			for (int j = 0; j < b; j++) {
				sum += a;
			}
		}
		//dp.
		dp[0][0] = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= sum; j++) {
				if (dp[i - 1][j] != 0) {
					for (int k = 0; k <= card[i - 1][1]; k++) {
						dp[i][j + k*card[i - 1][0]] += dp[i - 1][j];
					}
				}
			}
		}
		//cin2.
		cin >> m;
		for (int i = 0; i < m; i++) {
			cin >> a;
			cout << dp[n][a] << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int M, G, dp[2][1010];

signed main() {
    while(cin >> M, M) {
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;

        rep(s,0,M) {
            int mo = s % 2;
            int a, b; cin >> a >> b;
            repq(k,0,b) {
                int cost = a * k;
                repq(i,0,1000) {
                    if(i-cost < 0) continue;
                    dp[mo^1][i] += dp[mo][i-cost];
                }
            }
            repq(i,0,1000) dp[mo][i] = 0;
        }

        cin >> G;
        rep(s,0,G) {
            int N; cin >> N;
            cout << dp[M%2][N] << endl;
        }
    }
    return 0;
}

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

int m, a[7], b[7];
int n;
long dp[8][2000];

long rec(int x, long y){ //x番目のカードについて考える, 今の合計はy
	if(dp[x][y]>=0) return dp[x][y];
	
	long ret=0;
	
	if(y>n) return 0;
	else if(y==n) return 1;
	else if(x==m) return 0;
	else{
		for(int i=0; i<=b[x]; ++i) ret+=rec(x+1, y+i*a[x]);
	}
	
	return dp[x][y]=ret;
}

int main(){
	while(1){	
		scanf(" %d", &m);
		if(m==0) break;
		for(int i=0; i<m; ++i) scanf(" %d %d", &a[i], &b[i]);
		
		int g;
		scanf(" %d", &g);
		for(int i=0; i<g; ++i){
			scanf(" %d", &n);	
			memset(dp, -1, sizeof(dp));
			printf("%ld\n", rec(0, 0));
		}
			
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll dp[100][10000];

int main() {
    int m;
    while (cin >> m && m) {
        int a[m], b[m];
        for (int i = 0; i < m; i++) {
            cin >> a[i] >> b[i];
        }
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 10000; j++) {
                dp[i][j] = 0;
            }
        }
        dp[0][0] = 1;
        for (int i = 0; i < m; i++) {
            for (int k = 0; k <= b[i]; k++) {
                for (int j = 0; j < 10000; j++) {
                    if (j >= k * a[i]) dp[i + 1][j] += dp[i][j - k * a[i]];
                }
            }
        }
        int g;
        cin >> g;
        for (int i = 0, n; i < g; i++) {
            cin >> n;
            cout << dp[m][n] << endl;
        }
    }
}

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

long long int dp[1001];
int a[8],b[8];
int main(void){
	int n,g;
	while(1){
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			scanf("%d%d",&a[i],&b[i]);
		}
		dp[0]=1;
		for(int i=0;i<n;i++){
			for(int j=1000;j>=0;j--){
				for(int k=1;k<=b[i];k++){
					if(a[i]*k<=j){
						dp[j]+=dp[j-a[i]*k];
					}
				}
			}
		}
		scanf("%d",&g);
		for(int i=0;i<g;i++){
			int a;
			scanf("%d",&a);
			printf("%d\n",dp[a]);
		}
	}
    return 0;
}