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 <cstring>

using namespace std;

const int MAX_M = 7;
const int MAX_G = 10;
const int MAX_K = 1000;
int m;  // カードの種類 1<= m <= 7
int a[MAX_M], b[MAX_M]; // a: カードに書かれた数字 b: 枚数
int g; // ゲームの回数
int n[MAX_G];
int K;
int dp[MAX_M + 1][MAX_K + 1];

void solve()
{
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;

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

int main()
{
    while (cin >> m, m) {
        for (int i = 0; i < m; i++)
            cin >> a[i] >> b[i];
      
        K = 0;
        cin >> g;
        for (int i = 0; i < g; i++) {
            cin >> n[i];
            K = max(K, n[i]);
        }
        solve();

        for (int i = 0; i < g; i++)
            cout << dp[m][n[i]] << endl;
    }


    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#ifndef M_PI
#define M_PI 3.1415926535
#endif
#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()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second

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

int ans[1001];

int fact(int n){
  if(n<2)return 1;
  return fact(n-1)*n;
}

main(){
  int m;
  while(cin>>m,m){
    memset(ans,0,sizeof(ans));

    rep(i,m){
      int a,b;
      cin>>a>>b;
      for(int j=1000;j>0;j--){
	if(ans[j]==0)continue;
	int u=0;
	for(int k=j+a;k<=1000 && u<b;k+=a){
	  u++;
	  ans[k]+=ans[j];
	}
      }
      rep(j,b)ans[a*j+a]+=1;
    }
    //rep(i,10)cout<<ans[i+1]<<endl;

    int n;
    cin>>n;
    while(n--){
      int a;
      cin>>a;
      cout<<ans[a]<<endl;
    }
  }
}

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 m;
int card[100+1];
int g;
int memo[1001][101];

int solve(int n, int u){
  if( n == 0 )
    return 1;
  if( n<0 || u>100 ) return 0;

  if( memo[n][u] != 0 )
    return memo[n][u];

  int res=0;
  REP(i,card[u]+1){
    res += solve( n-u*i, u+1 );
  }

  return memo[n][u] = res;
}


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

  while( cin>>m, m ){
    memset( memo,0,sizeof(memo) );
    memset( card,0,sizeof(card) );
    REP(_t,m){
      int a,b;
      cin >>a>>b;
      card[a] = b;
    }

    cin >> g;
    REP(_t,g){
      int n;
      cin >> n;
      cout << solve(n, 0) << endl;
    } 
  }

  return 0;
}

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

using namespace std;

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

int solve(){
  fill(dp[0], dp[8], 0);
  dp[0][0] = 1;
  for(int i=0;i<m;i++){
    for(int j=0;j<1001;j++){
      if(dp[i][j] == 0) continue;
      for(int k=0;k<=b[i];k++){
	int tmp = j+a[i]*k;
	if(tmp > 1000) continue;
	dp[i+1][tmp] += dp[i][j];
      }
    }
  }
}

main(){
  while(cin >> m && m){
    for(int i=0;i<m;i++){
      cin >> a[i] >> b[i];
    }
    cin >> g;
    solve();
    for(int i=0;i<g;i++){
      cin >> n;
      cout << dp[m][n] << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int m,g,a[7],b[7],c,n;
void rec(int k,int s){
	rep(i,b[k]+1){
		int t=s+a[k]*i;
		if(t<n&&k<m-1)rec(k+1,t);
		if(t==n)c++;
	}
}
int main(){
	while(cin>>m,m){
		rep(i,m)cin>>a[i]>>b[i];
		cin>>g;
		rep(i,g){
			c=0; cin>>n;
			rec(0,0);
			cout<<c<<endl;
		}
	}
	return 0;
}

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

#define MAX_N 82000
#define MAX_CARD 100

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 <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int m;
    while (cin >> m, m) {
        int mx = 1001;
        vector<int> a(m), b(m);
        REP(i, 0, m) {
            cin >> a[i] >> b[i];
        }
        auto dp = make_v(m + 1, mx, 0LL);
        dp[0][0] = 1;
        REP(i, 0, m) {
            REP(j, 0, mx) {
                REP(k, 0, b[i] + 1) {
                    if (j + k * a[i] < mx) {
                        dp[i + 1][j + k * a[i]] += dp[i][j];
                    }
                }
            }
        }

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