1+ import java .io .BufferedReader ;
2+ import java .io .IOException ;
3+ import java .io .InputStreamReader ;
4+
5+ /*
6+
7+ QUESTION:
8+
9+ Chef and Divyam are playing a game with the following rules:
10+
11+ First, an integer X! is written on a board.
12+ Chef and Divyam alternate turns; Chef plays first.
13+ In each move, the current player should choose a positive integer D which is divisible by up to Y distinct primes
14+ and does not exceed the integer currently written on the board. Note that 1 is not a prime.
15+ D is then subtracted from the integer on the board, i.e. if the integer written on the board before this move was A,
16+ it is erased and replaced by A−D.
17+ When one player writes 0 on the board, the game ends and this player wins.
18+ Given X and Y, determine the winner of the game.
19+ */
20+
21+ public class PrimeGame {
22+
23+ private static final int NUMBER = 1000000 ;
24+ private static final int [] array = new int [NUMBER + 1 ];
25+ private static final BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
26+
27+ public static void main (String [] args ) throws IOException {
28+
29+ int t ;
30+ int chefPlay ;
31+ int divyamPlay ;
32+
33+ boolean [] primes = getPrimesBySieveOfEratosthenes (NUMBER );
34+ setArrayWithPrimes (primes );
35+
36+ t = Integer .parseInt (br .readLine ());
37+
38+ while (t -- >= 0 ) {
39+
40+ String line = br .readLine ();
41+ String [] strings = line .split (" " );
42+
43+ chefPlay = Integer .parseInt (strings [0 ]);
44+ divyamPlay = Integer .parseInt (strings [1 ]);
45+
46+ checkWinner (chefPlay , divyamPlay );
47+ }
48+
49+ br .close ();
50+ }
51+
52+
53+ /*
54+ Receives 2 plays and checks and prints the winner
55+ */
56+ private static void checkWinner (int chefPlay , int divyamPlay ) {
57+ if (array [chefPlay ] <= divyamPlay ) {
58+ System .out .println ("Chef" );
59+ }
60+ else {
61+ System .out .println ("Divyam" );
62+ }
63+ }
64+
65+ /*
66+ Receives a integer number and defines an boolean array with all primes smaller than or equal to
67+ the number using Sieve of Eratosthenes algorithm
68+ link: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
69+ */
70+ private static boolean [] getPrimesBySieveOfEratosthenes (int number ) {
71+
72+ /*
73+ Create a boolean array "prime[0..n]" and initialize
74+ all entries it as true. Each prime[i] will
75+ finally be false if i is Not a prime, else true.
76+ */
77+ boolean [] prime = new boolean [number + 1 ];
78+
79+ for (int i = 0 ; i <= number ; i ++) {
80+ prime [i ] = true ;
81+ }
82+
83+ for (int p = 2 ; p * p <= number ; p ++) {
84+
85+ // If prime[p] is not changed, then it is a prime
86+ if (prime [p ]) {
87+ // Update all multiples of p
88+ for (int i = p *p ; i <= number ; i += p ) {
89+ prime [i ] = false ;
90+ }
91+ }
92+ }
93+
94+ return prime ;
95+ }
96+
97+ /*
98+ Sets the array according to the givens primes array
99+ */
100+ private static void setArrayWithPrimes (boolean [] primes ) {
101+
102+ array [0 ] = 0 ;
103+ array [1 ] = 0 ;
104+
105+ for (int i = 2 ; i <= NUMBER ; i ++) {
106+ array [i ] = array [i - 1 ];
107+
108+ if (primes [i ]) {
109+ array [i ] = array [i ] + 1 ;
110+ }
111+ }
112+ }
113+
114+ }
0 commit comments