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(easy) » Number Game Revisited

Number Game Revisited

Problem code: NUMGAME2

  • Submit
  • All Submissions

All submissions for this problem are available.

Alice and Bob play the following game.They choose a number N to play with.The runs are as follows :

1.Bob plays first and the two players alternate.

2.In his/her turn ,a player can subtract from N any prime number(including 1) less than N.The number thus obtained is the new N.

3.The person who cannot make a move in his/her turn loses the game.

Assuming both play optimally,who wins the game ?

Input format:

The first line contains the number of test cases T.Each of the next lines contains an integer N.

Output format:

Output T lines one for each test case,containing "ALICE" if Alice wins the game ,or "BOB" if Bob wins the game.

Example

Sample Input:
2
1
2

Sample Output: ALICE BOB
Constraints: 1 <= T <= 1000000 1 <= N <= 1000000000

Note : For the first test case, Bob cannot make any move and hence Alice wins the game. For the second test case, Bob subtracts 1 from N. Now, Alice cannot make a move and loses the game.


Author: nssprogrammer
Tester: rajivka
Editorial http://discuss.codechef.com/problems/NUMGAME2
Date Added: 23-11-2010
Time Limit: 1 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, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

how is the if(n%4==1)

navneet604779 @ 10 Oct 2011 02:45 PM
how is the if(n%4==1) ===>alice else ===>bob is true cause for values like 7,11,15,19,23 alice should be winning but the result given is bob????? also I did not understand what property is used???

i got IT how it works!!!

navneet604779 @ 10 Oct 2011 03:28 PM
i got IT how it works!!!

Where can I get more Test

vanwilder @ 16 Oct 2011 01:13 PM
Where can I get more Test cases to check my problem against them?

Please explain me this....

ashu1 @ 8 Jun 2012 12:35 PM
Please explain me this....

Any one can please explain me

mihirnanavati @ 4 Jul 2012 08:38 PM
Any one can please explain me this mod 4 logic?????thnx in advance

hey can anyone tell me dat

sid12 @ 12 Jul 2012 04:04 PM
hey can anyone tell me dat does optimal solution means number of steps shud be least???

hey...cn u plz tell tht how

anick_gemini @ 6 Aug 2012 01:31 AM
hey...cn u plz tell tht how the %4 logic is working... take n = 7 bob hits first... he subtracts 5...(a prime) n=2; alice subtracts 1; now, n=1; bob loses... then hws %4 logic working ???? plz explain

@anick_gemini Both players

rahullak @ 15 Aug 2012 09:10 AM
@anick_gemini Both players play optimally. For n=7, Bob will choose 2 first. n=5, no matter what Alice chooses, Bob will win. You have to work it out for n=1,2,3,5. The rest follows automatically.

@adm why 1 cant be choosen as

justme686 @ 17 Sep 2012 07:18 PM
@adm why 1 cant be choosen as the first prime no to be removed.....

I am not getting the logic of

cnatka @ 4 Nov 2012 12:48 PM
I am not getting the logic of %4. Please tell me the logic behind it. It seems working but the reason i am unable to understand!

If you try to solve the

farazahmad @ 16 Nov 2012 06:35 AM
If you try to solve the problem by playing optimally, you will see that if N = 1,5,9,13,17,....., the first player will lose, otherwise first player will win. e.g., if N = 10,11, or 12 on first player's turn, he can subtract 1,2, and 3 (all primes or 1) respectively to convert it to 9 for other player. Working backwards, 9 will work out to be a losing scenario. 1,5,9,13,17,..... turn out to be (multiples of 4) + 1. That's where the logic of %4 is coming.

How to get to the reasoning

programme @ 18 Dec 2012 10:24 PM
How to get to the reasoning of n%4 intuitively?

Please dont post answers...

codersrikant @ 24 Jan 2013 09:54 PM
Please dont post answers...

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.