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++: // 2011/11/02 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int m;
		cin >> m;
		if(m==0)
			break;

		int DP[8][1001];
		DP[0][0] = 1;
		for(int j=1; j<=1000; j++)
			DP[0][j] = 0;

		for(int i=1; i<=m; i++){
			for(int j=0; j<=1000; j++)
				DP[i][j] = DP[i-1][j];

			int a, b;
			cin >> a >> b;

			for(int j=0; j<=1000; j++){
				if(DP[i-1][j]==0)
					continue;

				for(int k=1; k<=b; k++){
					if(j+a*k>1000)
						break;

					DP[i][j+a*k] += DP[i-1][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<bits/stdc++.h>
using namespace std;
main(){
  int n,k;
  while(1){
    cin>>n;
    if(n==0)break;
    int num[50];
    int numnum[50];
    int cnt=0;
    int g;
    for(int i=0;i<n;i++){
      cin>>num[i]>>numnum[i];
    }
    cin>>g;
    for(int i=0;i<g;i++){
      int y;
      cin>>y;
      cnt=0;
      queue<int> que;
      que.push(0);
      for(int i=0;i<n;i++){
	int size=que.size();
	for(int j=0;j<size;j++){
	  int now=que.front();
	  que.pop();
	  for(int k=0;k<=numnum[i];k++){
	    if(now+num[i]*k==y){
	      cnt++;
	    }
	    else{
	      que.push(now+num[i]*k);
	    }
	  }
	}
      }
      cout<<cnt<<endl;
    }
  }
}

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

int a[7], b[7], m;

//次i
int rec(int n, int i){
	if(n == 0){
		return 1;
	}

	if(n < 0 || i > m){
		return 0;
	}

	int res = 0;
	for(int j=0;j<=b[i];j++){
		res += rec(n-a[i]*j, i+1);
	}

	return res;
}

int main(){
	while(std::cin >> m, m){
		for(int i=0;i<m;i++){
			std::cin >> a[i] >> b[i];
		}
		std::sort(a, a+m);
		int g, n;
		std::cin >> g;
		for(;g--;){
			std::cin >> n;
			std::cout << rec(n, 0) << std::endl;;
		}
	}
}

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

int m, g, n;
vector<int> A, B;
long long dp[10][8000];

long long solve(int k, int sum, int goal) {
    if (0 <= dp[k][sum]) {
        return dp[k][sum];
    }
    if (sum == goal) {
        return 1;
    }
    if (goal < sum || (int)A.size() <= k) {
        return 0;
    }
    long long ret = 0;
    for (int i=0; i<=B[k]; ++i) {
        ret += solve(k+1, sum + A[k] * i, goal);
    }
    return dp[k][sum] = ret;
}

int main() {
    while (cin >> m, m) {
        A.resize(m);
        B.resize(m);
        for (int i=0; i<m; ++i) {
            cin >> A[i] >> B[i];
        }
        cin >> g;
        for (int i=0; i<g; ++i) {
            cin >> n;
            memset(dp, -1, sizeof dp);
            cout << solve(0, 0, n) << endl;
        }
    }
    return 0;
}

C++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < ((k)+(n)); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(const _Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ "<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;cout<<" ]"<<endl; }
}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

int card[10], num[10];
ll dp[100][1001];
int k;

void solve(int m){
    FILL(dp,0);
    dp[0][0] = 1; k = 0;
    REP(i,m){
        REP(j,num[i]+1){
            int c = card[i] * j;
            REP(x,1001){
                if(dp[k][x] <= 0) continue;
                if(x+c <= 1000)
                    dp[k+1][x+c] += dp[k][x];
            }
        }
        REP(i,50) cout << dp[k+1][i] << " "; cout << endl;
        k++;
    }
}

int main(){
    int m, g;
    while(cin >> m && m){
        REP(i,m){
            cin >> card[i] >> num[i];
        }
        solve(m);
        cin >> g;
        REP(i,g){
            int n;
            cin >> n;
            cout << dp[k][n] << endl;
        }
    }
    return 0;
}

C++: #include<iostream>

using namespace std;

int main(void){
  int m,a[8],b[8],g,n,dp[8][1001];

  while(cin >> m,m){

    for(int i=0;i<8;i++){
      for(int j=0;j<1001;j++){
	dp[i][j]=0;
      }
    }

    for(int i=0;i<m;i++)
      cin >> a[i] >> b[i];
    
    for(int j=0;j<=b[0];j++){
      dp[0][a[0]*j]++;
    }
   

    for(int i=0;i<m-1;i++){
      for(int j=0;j<1001;j++){
	  for(int k=0;k<=b[i+1];k++){
	    if(j+(a[i+1]*k)<1001)dp[i+1][j+(a[i+1]*k)]+=dp[i][j];
	  }
      }
    }

    for(int i=0;i<m;i++){
      for(int j=0;j<20;j++){
	cout << dp[i][j] <<" ";
      }
      cout << endl;
    }

    cin >> g;
    while(g--){
      cin >> n;
      cout << dp[m-1][n] << endl;
    }
  }

  return 0;
}