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 <vector>
#include <list>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))
 
using namespace std; 

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 1001;

int main(void){
    for(int m; cin >> m, m;){
        vi dp(N);
        dp[0] = 1;

        while(m --){
            int a, b; cin >> a >> b;
            for(int i = N - 1; 0 <= i; i --)
                if(dp[i])
                    COUNT(j, b)
                        if(i + a * j < N) dp[i + a * j] += dp[i];
        }

        int g; cin >> g;
        while(g --){
            int n; cin >> n;
            cout << dp[n] << endl;
        }
    }
 
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <sstream>
#include <queue>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int m,num[7],val[7],n,solve;

void dfs(int sum,int pos){

	if(sum == n && pos == m)solve++;

	if(sum > n || pos >= m)return;
	
	for(int i=0;i<=val[pos];i++){
		dfs(sum+(i*num[pos]) , pos+1);
	}
}
int main(){
	while(cin >> m ,m){
		rep(i,m){
			int a,b;
			cin >> num[i] >> val[i];
		}
		
		int g; cin >> g;
		rep(i,g){
			cin >> n;
			solve = 0 , dfs(0,0);
			cout << solve << endl;
		}
	}
}

C++: #include <iostream>
#include <cmath>
#include <string>

using namespace std;

const int MAX_M = 7, MAX_B = 10, MAX_N = 1000;
int m,g,n;
int a[MAX_M], b[MAX_M];
int dp[MAX_M + 1][MAX_N + 1];

int main()
{
    while(cin >> m && m) {
        for(int i = 0; i < m; i++)
            cin >> a[i] >> b[i];

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

        for(int i = 0; i < m; i++) {
            for(int j = 0; j <= MAX_N; j++) {
                for(int k = 0; k <= b[i]; k++) {
                    if(j + a[i] * k > 1000) continue;
                    dp[i+1][j+a[i]*k] += dp[i][j];
                }
            }
        }
        
        cin >> g;
        for(int k = 0; k < g; k++) {
            cin >> n;
            cout << dp[m][n] << endl;
        }

        
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int m, g;
int dp[8][1024];


int main(){
  while(scanf("%d", &m) && m){
    int a[7], b[7];
    rep(i, m) scanf("%d%d", a+i, b+i);

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

    rep(i, m){
      rep(j, 1000){
	//	fprintf(stderr, "%d\n", j);
	rep(k, b[i]+1){
	  if(j+a[i]*k > 1000) continue;
	  dp[i+1][j+a[i]*k] += dp[i][j];
	}
      }
    }

    /*    rep(i, 7){
      rep(j, 50) printf("%d ", dp[i][j]);
      puts("");
      }*/
    scanf("%d", &g);
    rep(i, g){
      int n;
      scanf("%d", &n);
      printf("%d\n", dp[m][n]);
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int memo[101][1024];
int rec(vint& num, int v, int p)
{
	if(v < 0) return 0;
	if(v == 0) return 1;
	if(p == 101) return 0;
	if(0 < memo[p][v]) return memo[p][v];
	
	int res = 0;
	rep(i, num[p] + 1) res += rec(num, v - p * i, p + 1);
	
	return memo[p][v] = res;
}

int main()
{
    int m, g, a, b, n;
	while(cin >> m && m)
	{	
		vint num(128, 0);
		memset(memo, -1, sizeof memo);
		
		rep(i, m)
		{
			cin >> a >> b;
			num[a] += b;
		}
		
		cin >> g;
		rep(i, g)
		{
			cin >> n;
			cout << rec(num, n, 0) << endl;
		}
	}
}

C++: #include <iostream>
#include <stdio.h>

using namespace std;

int m,cn[8],ck[8],an[10000];

void cal(int s,int k) {
	int i;
	if (k==m) return;
	for (i=0;i<=ck[k];i++) {
		if (i>0) an[s+i*cn[k]]++;
		cal(s+i*cn[k],k+1);}
}

int main() {
 int i,g,n;
 while(cin >> m) {
	 if (m==0) break;
	 for (i=0;i<m;i++) cin >> cn[i] >> ck[i];
     for (i=0;i<10000;i++) an[i]=0;
	 cal(0,0);
	 cin >> g;
	 for (i=0;i<g;i++) { cin >> n; cout << an[n] << endl; }
 }
return 0;
 }

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

int a[7];
int b[7];
int m;
long long int dp[7][11][1001];
long long int check(int i,int cnt,int n)
{
  //  cout<<m<<endl;
  //  cout<<i<<" "<<cnt<<" "<<n<<endl;

  if(n==0)
    return 1;
  if(n<0 || i>=m)
    return 0;
  if(dp[i][cnt][n]!=-1)
    return dp[i][cnt][n];
		 
  long long int ans = 0;
  if(b[i]>cnt)
    ans+=check(i,cnt+1,n-a[i]);
  ans+=check(i+1,0,n);
  dp[i][cnt][n]=ans;
  return ans;
  
}

int main()
{
  for(;cin>>m,m;)
    {
      //      card.clear();
      for(int i=0;i<m;i++)
	{
	  cin>>a[i]>>b[i];
	}
      int g;
      cin>>g;
      for(;g--;)
	{
	  int n;
	  cin>>n;
	  memset(dp,-1,sizeof(dp));
	  cout<<check(0,0,n)<<endl;
	}
    }
}