CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Challenge 2013
    • May Cook-Off 2013
    • May Challenge 2013
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • Campus Chapters
    • Host your Contest
    • Go for Gold
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
    • Top Contributors on Discuss
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(Peer) » Stone Game

Stone Game

Problem code: RESN04

  • Submit
  • All Submissions

All submissions for this problem are available.

Alice and Bob play the following game : There are N piles of stones with Si stones in the ith pile. Piles are numbered from 1 to N. Alice and Bob play alternately, with Alice starting. In a turn, the player chooses any pile i which has atleast i stones in it, and removes exactly i stones from it. The game ends when there is no such pile. The player who plays last wins the game. Assuming Alice and Bob play optimally, who will win the game?

Input

The first line contains the number of test cases T (<= 100). There follow 2T lines, 2 per test case. The first line of each test case conains N (<= 100), the number of piles. The second line contains N space seperated integers, specifying the number of stones in pile 1, pile 2, ..., pile N. There will be atleast 1 and atmost 1000 stones in any pile.

Output

Output T lines, one per test case. For each test case, output "ALICE" if Alice wins the game, and "BOB" if Bob wins the game.

Example

Input:
2
1
1
2
1 1

Output:
ALICE
ALICE


Author: admin
Date Added: 18-12-2009
Time Limit: 5 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

I think my code is giving the

manjotpahwa @ 16 May 2010 05:33 PM

I think my code is giving the right answer, I don't know why CodeChef is showing Wrong Answer. Here is my code:

#include<stdio.h>
#include<stdlib.h>

int stones[100] = {0};

int main()
{
int test, i, piles, count;
scanf("%d", &test);
while(test--)
{
count = 0;
scanf("%d", &piles);
for(i=0;i<piles;i++)
scanf("%d", &stones[i]);

for(i=0;i<piles;i++)
{//number of stones will be zero after S turns, cuz Si stones in each pile
count = count + stones[i]/(i+1);
}//for ends
if(count%2==0)
printf("BOB");
else printf("ALICE");

}//while ends
return 0;
}//main ends

you are not printing end of

prashanthkvs @ 16 May 2010 06:20 PM

you are not printing end of line n.

can someone check this out

code.adimania @ 28 Jun 2010 01:41 AM

can someone check this out and lemme know my mistake?

http://www.codechef.com/viewsolution/273267

Your code gives the wrong

triplem @ 28 Jun 2010 11:30 AM

Your code gives the wrong output for the sample input.

Can any body please explain

default1130 @ 7 Oct 2010 07:47 PM

Can any body please explain the logic.

#include<stdio.h>#include<std

dhananjay.nitjsr @ 11 Oct 2010 04:06 PM

#include<stdio.h>
#include<stdlib.h>

int stones[100] = {0};

int main()
{
int test, i, piles, count;
scanf("%d", &test);
while(test--)
{
count = 0;
scanf("%d", &piles);
for(i=0;i<piles;i++)
scanf("%d", &stones[i]);

for(i=0;i<piles;i++)
{//number of stones will be zero after S turns, cuz Si stones in each pile
count = count + stones[i]/(i+1);
}//for ends
if(count%2==0)
printf("BOBn");
else printf("ALICEn");

}//while ends
return 0;
}//main ends

wat does playing optimally

spand89 @ 16 Oct 2010 06:28 PM

wat does playing optimally mean????????

guys...wat does optimal means

bLANK thoughtS @ 3 Nov 2010 11:26 PM

guys...wat does optimal means ??

http://dictionary.reference.c

triplem @ 4 Nov 2010 01:55 AM

http://dictionary.reference.com/browse/optimal

hi friends below programme

dinu1405 @ 12 Nov 2010 03:32 PM

hi friends below programme was giving the correct output for th input can any one please tell me why it is wrong

 

#include <stdio.h>
#include <stdlib.h>
int main()
{
int no_of_piles,*stones_in_piles,counter1,counter2;
int player,first_chance,no_of_testcases;
int *winner;
scanf("%d",&no_of_testcases);
winner = (int *)malloc(sizeof(int) * no_of_testcases);
for(counter2=0;counter2<no_of_testcases;counter2++)
{
player = 1;//1 = Alice , 0 = Bob
first_chance = 1;
scanf("n%d",&no_of_piles);
stones_in_piles = (int *)malloc(sizeof(int) * no_of_piles);
for(counter1=0;counter1<no_of_piles;counter1++)
{   
scanf("%d",&stones_in_piles[counter1]);
}
while(1)
{
for(counter1=0;counter1<no_of_piles;counter1++)
{
if(stones_in_piles[counter1]>=(counter1+1))
{
stones_in_piles[counter1]=stones_in_piles[counter1]-(counter1+1);
if(player)
player = 0;
else
player = 1;
first_chance = 0;
break;
}
}
if(counter1 == no_of_piles)
{
if(first_chance)
{
printf("nNo one wins as no piles sufficient to play");
free(stones_in_piles);
break;
}
if(player)
player = 0;
else
player = 1;
winner[counter2]=player;
free(stones_in_piles);
break;
}
}
}
for(counter2=0;counter2<no_of_testcases;counter2++)
{
if(winner[counter2])
printf("Alicen");
else
printf("Bobn");

}
free(winner);
}

i got it right actually

dinu1405 @ 12 Nov 2010 05:43 PM

i got it right actually earlier it was wrong because i printed Alice and Bob in Sentence case and not in upper case

Thanks a lot

I din't understood the

lorzm @ 29 Nov 2010 08:44 PM

I din't understood the problem, is too complicated for me.

Can anyone tell why this code

bayer_villager @ 17 Dec 2010 08:44 PM
Can anyone tell why this code is not giving right answer on codechef
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n=0,m=0, t=0, i=0,j=0,k=0,p=0;
cin>>t;
int num[t];
for(i=0;i<t;i++)
{
cin>>n;
for(k=0,j=1;j<=n;j++)
{
cin>>p;
m=p/j;
num[i]+=m;
}
}
for(int l=0;l<i;l++)
{
if(num[l]%2==0)
cout<<"BOB"<<endl;
else
cout<<"ALICE"<<endl;
}
getch();
return 0;
}

sorry  i have not used

bayer_villager @ 17 Dec 2010 08:47 PM

sorry  i have not used #include<conio.h> and getch() in my submission .It is giving wrong answer.

Where do you initialise the

triplem @ 18 Dec 2010 03:58 AM

Where do you initialise the values of num?

some one please tell me

harshit_sethi @ 20 Dec 2010 10:43 PM

some one please tell me what's the problem with my code?

it's running fine on my computer but when i submit it ,codechef indicates runtime error.

 

http://www.codechef.com/comment/reply/498643

Here's my code.It's simple

harshit_sethi @ 20 Dec 2010 10:45 PM

Here's my code.It's simple enough.but i am not able to figure out any flaw in it.I will be pleased to get a feedback from you.

 

 

#include<stdio.h>
#include<malloc.h>

int main()
{
int T,N,*stones,i,j,count=0;
char* str;

scanf("%d",&T);
str=(char *)calloc(T,sizeof(char));


for(i=0;i<T;i++)
{
scanf("%d",&N);
stones=(int*)calloc(N,sizeof(int));




for(j=0;j<N;j++)
{
scanf("%d",&stones[j]);

}

for(j=0;j<N;j++)
{
if(stones[j]>=j+1)
count++;
}

if(count%2==0)
str[i]='B';
else
str[i]='A';

count=0;

}






for(i=0;i<T;i++)
{
if(str[i]=='B')
printf("BOBn");

else
printf("ALICEn");


}



}

I want  the codechef team to

harshit_sethi @ 21 Dec 2010 12:22 AM

I want  the codechef team to clarify their output pattern.

I tried submitting my code for an hour and finally was able to catch the mistake that i used printf command after all the  test cases to print either ALICE or BOB for each test case.

 

I don't think this thing is clear in the problem.Hope my this comment helps someone and improves the clarification of output pattern in furthur problems.

It makes no difference

triplem @ 21 Dec 2010 01:42 AM

It makes no difference whatsoever at what point during your program you print output. This is clearly mentioned in the FAQ.

You were getting a runtime error because your main function wasn't returning 0. That is also clearly mentioned in the FAQ.

Your wrong answers were because you were outputting the wrong answers.

@Stephen: Sorry to bother

rajneesh2k10 @ 28 Dec 2010 02:20 PM
@Stephen: Sorry to bother again. :) What's wrong with my solution? http://www.codechef.com/viewsolution/408180 Its running very fine on the sample input! Any test case you please suggest to check on.

If the last pile has at least

triplem @ 29 Dec 2010 02:10 AM

If the last pile has at least n stones you seem to ignore every other pile entirely. That can't be right.

@Stephen: I don't think I

rajneesh2k10 @ 29 Dec 2010 08:44 AM

@Stephen: I don't think I ever said thanks so many times to a person!!

Thank you so much!

It was a blunder!

#include<stdio.h> int

eagleye @ 4 Jan 2011 02:40 PM

#include<stdio.h>

int main()

{

int a[100],stone[100]={0},c[100]={0},n,i,j,k=0,pile;

printf("enter the number of test cases:");

scanf("%d",&n);

for(i=0;i<n;i++)

{

scanf("%d",&pile);

for(j=0;j<pile;j++)

scanf("%d",&stone[j]);

for(j=0;j<pile;j++)

c[k]=c[k]+stone[j];

k++;

}

for(j=0;j<n;j++)

{

if((c[j])%2==0)

printf("BOBn");

else

printf("ALICEn");

}

for(j=0;j<100;j++)

c[j]=0;

return 0;

}

 

@stephen sir:what is wrong in this code ???

#include<stdio.h>int main(){

jayakrish @ 5 Feb 2011 07:18 PM

#include<stdio.h>
int main()
{
int i,s,t,n,u;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
u=0;
for(i=1;i<=n;i++)
{
scanf("%d",&s);
u^=s/i&1;
}
if (u)
printf("ALICEn");
else
printf("BOBn");
}
return 0;
}

This was one of the solution given in all solutions for this problem.

But for the following input

1

3

1 2 3

Output

ALICE

But  BOB can always win the game if he plays optimally.

 

Am i correct ?

 

 

No.

triplem @ 6 Feb 2011 12:49 AM

No.

IT IS UNFORTUNATE THAT "ALICE

sutanug @ 18 Feb 2011 02:28 AM

IT IS UNFORTUNATE THAT "ALICE " INSTEAD OF "Alice" IS GIVING WRONG ANSWER; ADMIN:PLEASE TAKE CARE OF IT........

There's nothing to take care

triplem @ 18 Feb 2011 05:36 AM

There's nothing to take care of. If you don't do what the problem statement clearly tells you to do, you will get wrong answer.

@meriam:could you please

turjachaudhuri @ 18 Mar 2011 01:52 PM

@meriam:could you please explain the logic behind this kind of solutions???

#include<stdio.h>

int main()

{

int a[100],stone[100]={0},c[100]={0},n,i,j,k=0,pile;

printf("enter the number of test cases:");

scanf("%d",&n);

for(i=0;i<n;i++)

{

scanf("%d",&pile);

for(j=0;j<pile;j++)

scanf("%d",&stone[j]);

for(j=0;j<pile;j++)

c[k]=c[k]+stone[j];

k++;

}

for(j=0;j<n;j++)

{

if((c[j])%2==0)

printf("BOBn");

else

printf("ALICEn");

}

admin could u plz teme y m i

luvshona @ 23 Apr 2011 03:02 AM

admin could u plz teme y m i gettin wrong answer for my submitted code? kindly check my submission

Code runs on my machine with

prakharbirla @ 30 Apr 2011 06:03 PM

Code runs on my machine with the Python Interpreter but CodeChef says there's a runtime error.. Why?

Use Python3

Have a look:

#!/usr/bin/env python

T = eval(input(""))
for case in range(0,T):
N = eval(input(""))
turns = 0
inp = input("")
stones = inp.split(None)
for pile in range(0,N):
if eval(stones[pile])>pile:
turns = turns + (eval(stones[pile])/(pile+1))
if turns % 2 == 0:
print("BOB")
else:
print("ALICE")

my code has been accepted

yashdv @ 15 Jun 2011 07:19 PM
my code has been accepted with time 0.0 seconds but i was just wondering why it is shown that i have used Mem = 2.5M i have seen some of the other codes with Mem = 0.0M and cant seem to get the reason for this event thought the codes are highly similar

Hello. Can somebody plz

yogeshjsy @ 14 Jul 2011 12:09 PM
Hello. Can somebody plz explain the problem to me. In second test case 2 1 1 2 piles with one stone in each. Alice picks the first one and Bob the second. How can Alice win?

hey there is a problem in

paramanantham @ 6 Aug 2011 12:19 AM
hey there is a problem in compiler since it doesnt recognize scanf for my submission

@admin Where's the problem in

navin25 @ 14 Aug 2011 08:38 PM
@admin Where's the problem in my code???

http://www.codechef.com/views

navin25 @ 17 Aug 2011 05:15 PM
http://www.codechef.com/viewsolution/625998

FAQ said that output file

tru3liar @ 2 Dec 2011 09:31 PM
FAQ said that output file contains only answers (and not the input lines) ,so in this question it doesn't matter how I input the number of stones in a pile . Did I understand FAQ right?

I had a NZEC error in java .I

tru3liar @ 2 Dec 2011 10:35 PM
I had a NZEC error in java .I was throwing an exception, without that the program has errors .How to work my way around this?

what if there are no such

ryker1990 @ 1 Mar 2012 01:51 AM
what if there are no such piles to play? i mean 0 piles with same number of stones as that of pile number;

import

ryker1990 @ 1 Mar 2012 01:54 AM
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class codechef { public static void main(String args[]) throws java.lang.Exception { int n[] = new int[100]; int i=1 ; int count,p=0; String winner[] =new String[100]; BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(buffer.readLine()); for(int j=0;j

please tell why the following

ujjwal12 @ 29 Mar 2012 10:09 PM
please tell why the following program is wrong.. http://www.codechef.com/viewsolution/932866

It says that game start with

teakay @ 30 Mar 2012 12:33 PM
It says that game start with Alice and in a turn, the player chooses any pile i which has atleast i stones in it, and removes exactly i stones from it. The game ends when there is no such pile. The player who plays last wins the game. From the testcase above isnt it suppose to be "BOB" instead of "ALICE" who wins the next round? I dont really get the game rule, could somebody exlpain? Thanks before

@ujjwal The printf statements

tussharsingh13 @ 30 Mar 2012 04:43 PM
@ujjwal The printf statements should be inside the first for loop and they should have a n at the end Also sum should be initialised to zero inside the first loop so that everytime it starts from zero :) :)

Can anyone tell me how this

exploit @ 25 Apr 2012 06:39 PM
Can anyone tell me how this logic works ?

what is the problem with this

sinha2011 @ 4 May 2012 12:20 AM
what is the problem with this solution? http://www.codechef.com/viewsolution/1009463

&ADMIN perhaps your compiler

mjnovice @ 29 May 2012 08:17 PM
&ADMIN perhaps your compiler treats "if(a)" different than "if(a>0)"....according to me both should produce the same results.....

My code is giving a run time

sagarchak @ 9 Jul 2012 05:13 PM
My code is giving a run time error. Can any1 clarify the problem. PLZ! http://fpaste.org/sEk8/

Hi i have a question ..! For

babbarshaer @ 11 Jul 2012 12:05 PM
Hi i have a question ..! For every Test does "alice" start the game or is it valid only for the first Test ..?

Hi babbarshaer,

samrat_gavale @ 24 Jul 2012 12:02 PM
Hi babbarshaer, for each test case, Alice will start the game.

sagarchak @ u r substracting

panicker @ 2 Aug 2012 12:29 PM
sagarchak @ u r substracting i from every input... some inputs are less than their index.. when u cannot further divide... try inpt/i

http://www.codechef.com/views

arpitraj22 @ 19 Aug 2012 03:27 PM
http://www.codechef.com/viewsolution/1268061 please check and point out mistake i have made :-( its executing fine on local but here its wrong ans.

Could someone tell me what is

dhruv_b @ 15 Dec 2012 01:06 AM
Could someone tell me what is wrong with my code? I am getting all the test cases right. http://www.codechef.com/viewsolution/1631290

Hey i submitted my code and

harryi3t @ 18 Dec 2012 04:28 PM
Hey i submitted my code and got 'wrong answer' but submitted same code on SPOJ and it got ACCEPTED!! can anyone help me (my code is very small, plz have a look (python 2.7)) http://www.codechef.com/viewsolution/1635721

SUCCESSFUL SUBMISSIONS


Fetching successful submissions

HELP



Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.

  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.

  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.

  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.

  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

  • If you are still having problems, see a sample solution here.
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.