Problem:
Problem G:
Nim
Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones on the table (we know the number of stones exactly). We play in turn and at each turn, you or I can remove one to four stones from the heap. You play first and the one who removed the last stone loses.
In this game, you have a winning strategy. To see this, you first remove four stones and leave 96 stones. No matter how I play, I will end up with leaving 92-95 stones. Then you will in turn leave 91 stones for me (verify this is always possible). This way, you can always leave 5
k
+ 1 stones for me and finally I get the last stone, sigh. If we initially had 101 stones, on the other hand, I have a winning strategy and you are doomed to lose.
Let's generalize the game a little bit. First, let's make it a team game. Each team has
n
players and the 2
n
players are seated around the table, with each player having opponents at both sides. Turns round the table so the two teams play alternately. Second, let's vary the maximum number ofstones each player can take. That is, each player has his/her own maximum number ofstones he/she can take at each turn (The minimum is always one). So the game is asymmetric and may even be unfair.
In general, when played between two teams of experts, the outcome of a game is completely determined by the initial number ofstones and the minimum number of stones each player can take at each turn. In other words, either team has a winning strategy.
You are the head-coach of a team. In each game, the umpire shows both teams the initial number of stones and the maximum number of stones each player can take at each turn. Your team plays first. Your job is, given those numbers, to instantaneously judge whether your team has a winning strategy.
Incidentally, there is a rumor that Captain Future and her officers of Hakodate-maru love this game, and they are killing their time playing it during their missions. You wonder where the stones are?. Well, they do not have stones but do have plenty of balls in the fuel containers!.
Input
The input is a sequence of lines, followed by the last line containing a zero. Each line except the last is a sequence of integers and has the following format.
n S
M
1
M
2
...
M
2
n
where
n
is the number of players in a team,
S
the initial number of stones, and
M
i
the maximum number of stones i th player can take. 1st, 3rd, 5th, ... players are your team's players and 2nd, 4th, 6th, ... the opponents. Numbers are separated by a single space character. You may assume 1 ≤
n
≤ 10, 1 ≤
M
i
≤ 16, and 1 ≤
S
≤ 2
13
.
Output
The out put should consist of lines each containing either a one, meaning your team has a winning strategy, or a zero otherwise.
Sample Input
1 101 4 4
1 100 4 4
3 97 8 7 6 5 4 3
0
Output for the Sample Input
0
1
1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n;
map <pair <int, int>, int> mp;

bool get_res(vector <int> &v, int id, int s) {
    if(s == 0) return 1;
    if(mp[{id, s}]) return mp[{id, s}];

    bool res = 0;
    for(int i = 1; i <= v[id]; i++)  {
        if(s - i < 0)
            break;
        res |= !(get_res(v, id % n + 1, s - i));
    }
    mp[{id, s}] = res;
    return res;
}

int main() {
    while(cin >> n) {
        if(n == 0) break;
        int s; cin >> s;
        mp.clear();
        n *= 2;
        vector <int> v(n + 1);
        for(int i = 1; i <= n; i++)
            cin >> v[i];
        cout << get_res(v, 1, s) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 10484
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
typedef long long int ll;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...)
#endif
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...)); }
template<typename T>
bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}
template<typename T>
bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}
// clang-format on

#include <functional>

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n, n) {
        int S;
        cin >> S;
        n *= 2;
        vector<int> a(n);
        REP(i, 0, n) {
            cin >> a[i];
        }

        auto memo = make_v(S + 1, n, -1);
        function<int(int, int)> rec = [&](int rest, int turn) {
            if (memo[rest][turn] != -1) return memo[rest][turn];
            if (rest == 0) {
                return 1;
            }
            int ans = 1;
            REP(i, 1, a[turn] + 1) {
                if (rest < i) continue;
                ans *= rec(rest - i, (turn + 1) % n);
            }
            return memo[rest][turn] = (ans == 0);
        };
        cout << rec(S, 0) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4176
final_rank: 15

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type{
	Sente,
	Gote,
	Unknown,
};

int N,stone_num;
int me[10],enemy[10];
Type memo[2][10][8193];

Type recursive(Type turn,int rest_num,int index){

	if(memo[turn][index][rest_num] != Unknown)return memo[turn][index][rest_num];

	Type result;

	if(turn == Sente){

		if(rest_num == 1){
			return memo[turn][index][rest_num] = Gote;
		}

		result = Gote;

		for(int num = 1; num <= me[index] && rest_num-num >= 1; num++){
			if(recursive(Gote,rest_num-num,index) == Sente){
				result = Sente;
				break;
			}
		}

	}else{

		if(rest_num == 1){
			return memo[turn][index][rest_num] = Sente;
		}

		result = Sente;

		for(int num = 1; num <= enemy[index] && rest_num-num >= 1; num++){
			if(recursive(Sente,rest_num-num,(index+1)%N) == Gote){
				result = Gote;
				break;
			}
		}
	}

	return memo[turn][index][rest_num] = result;
}


void func(){
	scanf("%d",&stone_num);

	for(int i = 0; i < 2; i++){
		for(int k = 0; k < N; k++){
			for(int p = 0; p <= stone_num; p++){
				memo[i][k][p] = Unknown;
			}
		}
	}

	for(int i = 0; i < N; i++){
		scanf("%d %d",&me[i],&enemy[i]);
	}

	if(recursive(Sente,stone_num,0) == Sente){
		printf("1\n");
	}else{
		printf("0\n");
	}
}


int main(){

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

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3840
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n,a[20],dp[1<<13][20];

int main() {
  while(cin >> n && n) {
    int m;
    cin >> m;
    n*=2;
    for(int i=0; i<n; i++) cin >> a[i];
    for(int i=0; i<n; i++) dp[0][i]=1;
    for(int t=1; t<=m; t++) {
      for(int i=0; i<n; i++) {
        dp[t][i]=0;
        for(int k=1; k<=a[i]; k++) {
          if(t-k>=0) dp[t][i]=max(dp[t][i],dp[t-k][(i+1)%n]^1);
        }
      }
    }
    cout << dp[m][0] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3500
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;

int n;
int s;
int m[20];

bool visited[(1<<14)][20];
int mem[(1<<14)][20];

int solve(int s,int id){
  if(visited[s][id]){
    return mem[s][id];
  }

  visited[s][id]=1;
  
  if( s==0 ){
    return mem[s][id]=id%2;
  }

  
  if(id%2==1){

    for(int i=1;i<=m[id];i++){
      if( solve( max(s-i,0) , (id+1)%(n+n) ) == 1 ){
        return mem[s][id]=1;
      }
    }
    return mem[s][id]=0;
    
  }else{

    for(int i=1;i<=m[id];i++){
      if( solve( max(s-i,0) , (id+1)%(n+n) ) == 0 ){
        return mem[s][id]=0;
      }
    }
    return mem[s][id]=1;
  }

  assert(0);
  return mem[s][id]=-1;  
}

int main(){
  while(1){
    cin>>n;
    if(n==0)break;
    memset( visited, false , sizeof(visited));
    cin>>s;
    for(int i=0;i<n+n;i++)cin>>m[i];
    cout<<1-solve(s,0)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4108
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
int n, S, M[22];
int dp[1<<15][22];
signed main(){
  while(cin >> n, n) {
    cin >> S;
    for(int i = 0; i < 2*n; i++) cin >> M[i];
    memset(dp, -1, sizeof(dp));
    for(int i = 0; i < 2*n; i++) dp[1][i] = 0;
    for(int i = 2; i <= S; i++){
      for(int j = 0; j < 2*n; j++) {
	bool flag = false;	
	for(int k = 1; k <= min(i, M[j]); k++) {
	  if(!dp[i-k][(j+1)%(2*n)]) flag = true;
	}
	dp[i][j] = (int)flag;
      }
    }
    cout << dp[S][0] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 8716
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600

bool memo[2<<14][35];
bool visited[2<<14][35];
vector<int> m;

bool dfs(int s, int t){
  if(visited[s][t]) return memo[s][t];
  visited[s][t]=true;
  if(s==0) return memo[s][t]=true;
  if(s==1) return memo[s][t]=false;
  int nt = (t+1)%m.size();
  repl(i,1,m[t]+1){
    if(dfs(s-i,nt)==false) return memo[s][t]=true;
  }
  return memo[s][t]=false;
}

int main(){
  int n;
  while(cin>>n, n){
    int s;
    cin>>s;
    m.resize(2*n);
    rep(i,2*n) cin>>m[i];

    fill(visited[0], visited[2<<14], false);
    if(dfs(s,0)) cout<<1<<endl;
    else cout<<0<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4840
final_rank: 18

code_snippet: #include<iostream>
using namespace std;
int s, n, a[25], dp[10000][25];
int solve(int r, int s) {
	if (r == 0) { dp[r][s] = 0; return 0; }
	if (dp[r][s] >= 0)return dp[r][s];
	int flag = 0;
	for (int i = 1; i <= a[s]; i++) {
		if (r - i < 0)continue;
		if (solve(r - i, (s + 1) % n) == false)flag = 1;
	}
	dp[r][s] = flag;
	return flag;
}
int main() {
	while (true) {
		for (int i = 0; i < 10000; i++) { for (int j = 0; j < 25; j++)dp[i][j] = -1; }
		cin >> n; if (n == 0)break; cin >> s; s--; n *= 2; for (int i = 0; i < n; i++)cin >> a[i];
		cout << solve(s, 0) << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4408
final_rank: 16

code_snippet: #include <vector>
#include <iostream>
using namespace std;
int n, s; vector<int> a; vector<vector<bool> > vis, dp;
bool solve(int x, int t) {
	if (vis[x][t]) return dp[x][t];
	bool ret = false;
	for (int i = 1; i <= a[t] && x - i > 0; i++) {
		if (!solve(x - i, (t + 1) % (2 * n))) ret = true;
	}
	vis[x][t] = true;
	return dp[x][t] = ret;
}
int main() {
	while (cin >> n, n) {
		cin >> s; a.resize(2 * n);
		for (int i = 0; i < 2 * n; i++) cin >> a[i];
		vis = vector<vector<bool> >(s + 1, vector<bool>(2 * n, false));
		dp = vector<vector<bool> >(s + 1, vector<bool>(2 * n));
		cout << solve(s, 0) << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4420
final_rank: 17

code_snippet: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int INF = 1<<28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int n, m[20], dp[20][1<<13 + 1];

int dfs(int idx, int rem){
	int &res = dp[idx][rem];
	if(res != -1) return res;
	if(rem == 1) return res = 0;
 
	FOR(i, 1, m[idx]+1) {
		if(rem - i < 1)
			break;
		if(!dfs((idx + 1) % n, rem - i))
			return res = 1;
	}
	return res = 0;
}
 
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	while(cin >> n, n) {
		n += n;
		int s; cin >> s;
		REP(i, n)
			cin >> m[i];
		memset(dp, -1, sizeof(dp));
		cout << dfs(0, s) << endl;
		/*
		REP(i, n*2) {
			REP(j, s)
				cout << dp[i][j] << ' ';
			cout << endl;
		}
		*/
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2736
final_rank: 8

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
#define MAX_N 10
#define MAX_S (1<<13)
 
int N,M[2*MAX_N];
int memo[2*MAX_N][MAX_S+1];
 
int solve(int p,int rem){
    if(rem == 0) return memo[p][rem] = 1;
    if(memo[p][rem] >= 0) return memo[p][rem];
    for(int i = 1 ; i <= M[p] ; i++){
	if(rem-i < 0) continue;
	if(solve((p+1)%N,rem-i) == 0){
	    return memo[p][rem] = 1;
	}
    }
    return memo[p][rem] = 0;
}
 
int main(){
    int S;
    while(cin >> N,N){
	cin >> S;
	N *= 2;
	for(int i = 0 ; i < N ; i++){
	    cin >> M[i];
	}
	memset(memo,-1,sizeof(memo));
	cout << solve(0,S) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3872
final_rank: 13

code_snippet: //Nim

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#define clr(a,v) memset((a),(v),sizeof(a))
const int MAX_N = 10, MAX_S = 1 << 13;

int n, S, M[2 * MAX_N];
int dp[2 * MAX_N][MAX_S + 1];

bool rec(int i, int j){
	if (j <= 1)return false;
	int &ret = dp[i][j];
	if (ret != -1)return ret;
	for (int k = 1; k <= M[i]; ++k){
		if (j - k >= 1){
			if (!rec((i + 1) % (n << 1), j - k))return ret = true;
		}
	}
	return ret = false;
}

int main(){
	while (scanf("%d", &n), n){
		scanf("%d", &S);
		for (int i = 0; i < (n << 1); ++i){
			scanf("%d", M + i);
		}
		clr(dp, -1);
		printf("%d\n", rec(0, S));

	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1928
final_rank: 3

code_snippet: // AOJ 1230 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1230)
#include<cstdio>
#include<cstring>
#define rep(i,a) for(int i=0;i<(a);++i)
#define clr(a,v) memset((a),(v),sizeof(a))

const int MAX_N = 10, MAX_S = 1<<13;

int n, S, M[2*MAX_N];
int dp[2*MAX_N][MAX_S+1];

bool rec( int i, int j )
{
	if( j <= 1 )
		return false;

	int &ret = dp[i][j];

	if( ~ret )
		return ret;

	rep( k, M[i] ) if( j-k-1 >= 1 ) if( !rec( (i+1)%(n<<1), j-k-1 ) )
		return ret = true;

	return ret = false;
}

int main()
{
	while( scanf( "%d", &n ), n )
	{
		scanf( "%d", &S );
		rep( i, n<<1 )
			scanf( "%d", M+i );

		clr( dp, -1 );
		printf( "%d\n", rec( 0, S ) );
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1912
final_rank: 4

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define MAX_N 10
#define MAX_S (1<<13)

int N,M[2*MAX_N];
int memo[2*MAX_N][MAX_S+1];

int solve(int p,int rem){
  if(rem == 0){ return memo[p][rem] = 1; }
  if(memo[p][rem] >= 0){ return memo[p][rem]; }
  for(int i = 1 ; i <= M[p] ; i++){
    if(rem-i < 0){ continue; }
    if(solve((p+1)%N,rem-i) == 0){
      return memo[p][rem] = 1;
    }
  }
  return memo[p][rem] = 0;
}

int main(){
  int S;
  while(cin >> N,N){
    cin >> S;
    N *= 2;
    for(int i = 0 ; i < N ; i++){
      cin >> M[i];
    }
    memset(memo,-1,sizeof(memo));
    cout << solve(0,S) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2060
final_rank: 6

code_snippet: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

/* struct P {
	int x, y, n;
	P(int n, int x, int y):n(n), x(x), y(y){}
	P(){}
}; */


/** Problem1230 : Nim **/
vector<int> lim;
int N;
const int MAX_N = 20, MAX_S = 1<<13;
int dp[MAX_S][MAX_N];


bool isWinning(int n, int p)
{
	if (n==1) return false;
	if (dp[n][p]!=-1) return dp[n][p];
	
	bool res = false;
	for (int i=1; i<=lim[p]; i++) {
		if (n-i>=0)
			res |= !isWinning(n-i, (p+1)%(2*N));
	}
	
	//cerr << n << " " << p << " " << res << endl;
	
	return dp[n][p]=res;
}

int main()
{
	int S;
	while (cin>>N, N) {
		cin>>S;
		memset(dp, -1, sizeof(dp));
		lim.resize(2*N);
		rep(i, 2*N) cin>>lim[i];
		
		cout << isWinning(S, 0) << endl;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 2284
final_rank: 7

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int c[30];
int n;
int dp[11000][21];
int solve(int a,int b){
	if(a<=0)return 1;
	if(~dp[a][b])return dp[a][b];
	for(int i=1;i<=c[b];i++){
		if(!solve(a-i,(b+1)%(n*2)))return dp[a][b]=1;
	}
	return dp[a][b]=0;
}
int main(){
	int a;
	int b;
	while(scanf("%d",&a),a){
		n=a;
		scanf("%d",&b);
		for(int i=0;i<a*2;i++)scanf("%d",c+i);
		for(int i=0;i<=b;i++)for(int j=0;j<a*2;j++)dp[i][j]=-1;
		printf("%d\n",solve(b,0));
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1732
final_rank: 1

code_snippet: #include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); ++i)

int solve(int n, int S, vector<int>& M){
    vector<vector<int> > dp(S+1, vector<int>(2*n));
    for(int i=2; i<=S; ++i)for(int j=0; j<(int)M.size(); ++j){
        for(int k=1; k<=M[j] && k<=i; ++k){
            if(!dp[i-k][(j+1)%(int)M.size()]){
                dp[i][j] = 1;
                break;
            }
        }
    }
    return dp[S][0];
}

int main(){
    int n;
    while(cin >> n, n){
        int S;
        cin >> S;
        vector<int> M(2*n);
        rep(i, 2*n)cin >> M[i];
        cout << solve(n, S, M) << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1800
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
using namespace std;

long long int GR[8200][21];

int main() {

  for ( long long int i = 0; i < 21; i++ ) {
    GR[1][i] = 0;
  }

  long long int n, S, input;

  while( true ) {

    cin >> n;

    if ( n == 0 ) break;

    cin >> S;

    vector< long long int > m;

    for ( long long int i = 0; i < n * 2; i++ ) {

      cin >> input;

      m.push_back( input );

    }

    for ( long long int i = 2; i <= S; i++ ) {

      for ( long long int j = 0; j < n * 2; j++ ) {

	bool flag[17] = {};

	for ( long long int k = max( 1LL, i - m[j] ); k < i; k++ ) {

	  flag[ GR[k][(j+1)%(n*2)] ] = true;

	}

	int cnt = 0;
	while( flag[cnt] ) {cnt++;}
	GR[i][j] = cnt;

      }

    }

    cout << ( GR[S][0] == 0 ? "0" : "1" ) << endl;

  }

  return 0;

}
accuracy: 1
time_ms: 10
memory_kb: 2112
final_rank: 5

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

int dp[1<<14][21];

int dfs(int total_players,int upto[21],
	int current_stones,int player_idx){
  if(dp[current_stones][player_idx] != -1){
    return dp[current_stones][player_idx];
  }
  if(current_stones == 0){
    return 1;
  }

  int res = 0;

  for(int use = 1; 
      use <= upto[player_idx] 
	&& current_stones - use >= 0; 
      use++){
    if(dfs(total_players,upto,
	   current_stones - use,
	   (player_idx + 1) % total_players) == 0){
      res = 1;
      break;
    }
  }
  return (dp[current_stones][player_idx] = res);
}

int main(){
  int total_players;
  while(~scanf("%d",&total_players)){
    if(total_players == 0) break;

    int initial_stones;
    scanf("%d",&initial_stones);

    int upto[21];
    memset(upto,0,sizeof(upto));
    for(int player_idx=0;player_idx < total_players * 2;player_idx++){
      scanf("%d",upto + player_idx);
    }

    memset(dp,-1,sizeof(dp));
    printf("%d\n",dfs(total_players * 2,upto,initial_stones,0));
  }
}
accuracy: 1
time_ms: 10
memory_kb: 2968
final_rank: 9

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

int dp[1<<14][21];

int dfs(int total_players,int upto[21],
	int current_stones,int player_idx){
  if(dp[current_stones][player_idx] != -1){
    return dp[current_stones][player_idx];
  }
  if(current_stones == 0){
    return 1;
  }

  int res = 0;

  for(int use = 1; 
      use <= upto[player_idx] 
	&& current_stones - use >= 0; 
      use++){
    if(dfs(total_players,upto,
	   current_stones - use,
	   (player_idx + 1) % total_players) == 0){
      res = 1;
    }
  }
  return (dp[current_stones][player_idx] = res);
}

int main(){
  int total_players;
  while(~scanf("%d",&total_players)){
    if(total_players == 0) break;

    int initial_stones;
    scanf("%d",&initial_stones);

    int upto[21];
    memset(upto,0,sizeof(upto));
    for(int player_idx=0;player_idx < total_players * 2;player_idx++){
      scanf("%d",upto + player_idx);
    }

    memset(dp,-1,sizeof(dp));
    printf("%d\n",dfs(total_players * 2,upto,initial_stones,0));
  }
}
accuracy: 1
time_ms: 20
memory_kb: 2968
final_rank: 10