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 <stdio.h>
#include <string.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int as[7];
int bs[7];
int m;
int sums[100 * 10 + 1];
void solve(int n, int sum) {
	if (sum > 1000)
		return;
	if (n == m) {
		sums[sum]++;
		return;
	}
	for (int i = 0; i <= bs[n]; i++) {
		solve(n + 1, sum + (i * as[n]));
	}
}

int main() {
	while (cin >> m, m) {
		memset(sums, 0, sizeof(sums));
		for (int i = 0; i < m; i++) {
			cin >> as[i];
			cin >> bs[i];
		}
		solve(0, 0);
		int g;
		cin >> g;
		for (int i = 0; i < g; i++) {
			int ni;
			cin >> ni;
			cout << sums[ni] << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int dp[8][7001]={};
int main(){
  int m,a[7],b[7],g,n;

  while(1){
    for(int i=0;i<=7;i++)
      for(int j=0;j<=7000;j++)
	dp[i][j]=0;

    cin>>m;

    if(m==0)break;
    for(int i=0;i<m;i++)
      cin>>a[i]>>b[i]; 
   
    cin>>g;

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

  return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int L = 1010;

int main() {
  int N;
  while(cin >>N && N){
    vector<P> v(N);
    REP(i, N) cin >>v[i].first >>v[i].second;
    vector<int> cnt(L, 0);
    cnt[0] = 1;
    REP(i, N){
      vector<int> tmp(L, 0);
      REP(j, L){
        if(cnt[j] <= 0) continue;
        REP(k, v[i].second) if(j + v[i].first * (k + 1) < L) tmp[j + v[i].first * (k + 1)] += cnt[j];
      }
      REP(j, L) cnt[j] += tmp[j];
    }
    int M; cin >>M;
    REP(i, M){
      int p; cin >>p;
      cout <<cnt[p] <<endl;
    }
  }
  return 0;
}

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

int a[11];
int b[11];
int dp[11][1010]; // xテ、ツサツ・テ、ツクツ凝」ツ?ョテ」ツつォテ」ツδシテ」ツδ嘉」ツ?ョテ」ツ?ソテ」ツつ津ァツ板ィテ」ツ??」ツ?ヲテヲツ閉ーyテ」ツ?ォテ」ツ?ェテ」ツつ凝」ツδ妥」ツつソテ」ツδシテ」ツδウテヲツ閉ー

int main() {
  int m;
  
  while( cin >> m, m ) {
    fill( a, a+10, 0 );
    fill( a, b+10, 0 ); 
    for( int i=0; i<m; i++ ) {
      int x, y;
      cin >> x >> y; a[i] = x, b[i] = y;
    }
    
    fill(dp[0], dp[10], 0);
    dp[0][0] = 1;
    for( int i=0; i<m; i++ ) {
      for( int j=0; j<1001; j++ ) {
	if( dp[i][j] ) {
	  for( int k=0; k<=b[i]; k++ ) {
	    if( j+k*a[i] <= 1000 ) dp[i+1][j+k*a[i]] += dp[i][j];
	  }
	}
      }
    }
    
    int g;
    cin >> g;
    for( int i=0; i<g; i++ ) {
      int q;
      cin >> q;
      cout << dp[m][q] << endl;
    }
  }
  
  return 0;
}

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


typedef long long ll;
const double Eps = 1e-4;

using namespace std;

int combination(int n, int k)
{
	ll res = 1;
	for (int i = 0; i < k; ++i)
		res *= n - i;
	for (int i = 2; i <= k; ++i)
		res /= i;
	return (int)res;
}
int main()
{
	int m;
	while (scanf("%d", &m), m)
	{
		int a[7], b[7];
		for (int i = 0; i < m; ++i)
			scanf("%d %d", a+i, b+i);

		int combi[1001];
		memset(combi, 0, sizeof(combi));
		combi[0] = 1;
		for (int i = 0; i < m; ++i)
		{
			int c[1001];
			memset(c, 0, sizeof(c));
			for (int j = 1; j <= b[i]; ++j)
				for (int k = 1000 - j*a[i]; k >= 0; --k)
					if (combi[k])
						c[k + j*a[i]] += combi[k];
			for (int i = 0; i < 1001; ++i)
				combi[i] += c[i];
		}
		int g;
		scanf("%d", &g);
		while (g--)
		{
			int n;
			scanf("%d", &n);
			printf("%d\n", combi[n]);
		}
	}

	return 0;
}

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

int a[8];
int b[8];
int dp[8][1001]; // x以下のカードのみを用いて数yになるパターン数

int main() {
  int m;
  
  while( cin >> m, m ) {
    fill( a, a+7, 0 );
    for( int i=0; i<m; i++ ) {
      cin >> a[i] >> b[i];
    }
    
    fill(dp[0], dp[7], 0);
    dp[0][0] = 1;
    for( int i=0; i<m; i++ ) {
      for( int j=0; j<1001; j++ ) {
	if( dp[i][j] ) {
	  for( int k=0; k<=b[i]; k++ ) {
	    if( j+k*a[i] <= 1000 ) dp[i+1][j+k*a[i]] += dp[i][j];
	  }
	}
      }
    }
    
    int g;
    cin >> g;
    for( int i= 0; i<g; i++ ) {
      int q;
      cin >> q;
      cout << dp[m][q] << endl;
    }
  }
  
  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>


void func(int m){
	int dp[1001][m+1],num,count,div;

	for(int i = 0; i <= 1000; i++){
		for(int k = 0; k <= m; k++)dp[i][k] = 0;
	}

	for(int i = m-1; i >= 0; i--){
		scanf("%d %d",&num,&count);

		for(int k = 1; k <= count;k++){
			div = num*k;
			if(dp[div][i] == 0){
				dp[div][i] = dp[div][i+1]+1;
			}else{
				dp[div][i]++;
			}

			for(int p = div+1; p <= 1000; p++){
				if(dp[p-div][i+1] > 0){
					if(dp[p][i] == 0){
						dp[p][i] = dp[p][i+1]+dp[p-div][i+1];
					}else{
						dp[p][i] += dp[p-div][i+1];
					}
				}
			}
		}
		for(int k = 1; k <= 1000; k++){
			if(dp[k][i] == 0 && dp[k][i+1] > 0)dp[k][i] = dp[k][i+1];
		}
	}

	int game_count,to_make;
	scanf("%d",&game_count);

	for(int i = 0; i < game_count; i++){
		scanf("%d",&to_make);
		printf("%d\n",dp[to_make][0]);
	}
}

int main(){

	int m;

	while(true){
		scanf("%d",&m);
		if(m == 0)break;

		func(m);
	}


	return 0;
}

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

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

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 <= 1000; ++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];
				}
			}
		}
		int n;
		cin >> n;
		for (int i = 0; i < n; i++) {
			int x;
			cin >> x;
			cout << dp[m][x] << endl;
		}
	}
	return 0;
}