Question D The game of NIM is fully discussed in WIKI: http://en

advertisement
Question D
The game of NIM is fully discussed in WIKI:
http://en.wikipedia.org/wiki/Nim
A game is in the winning configuration if the xor of all the numbers of stones in the piles is 0. So
we need to find such location. Let's look at the example of K=4 and L=13. The prime numbers
less than or equal to 13 are: 2, 3, 5, 7, 11,13.
A winning configuration is one with at least 2 piles and the xor of all the number of stones in the
piles is 0.
We need to store how many configurations are there so that the xor of the 4 prime numbers is
zero. The following are some of the examples:
2,2,3,3
2,3,11,13
....
We need to store the number of each values resulting from finding the xor of the numbers in
each iteration.
First, we need to notice that the xor of two prime numbers can be either prime numbers or nonprime numbers. So although we start with all prime numbers, the intermediate values can be
non-prime.
When two n-bit numbers are xor together, the value should be an n-bits number. Therefore, we
need to find out the minimum number of bits needed to represent the prime numbers and
those intermediate values. In the example, the largest prime number is 13 which is 4 bits. So
the intermediate values can be from 0 to 15.
The first thing to do so is to find a matrix A={๐‘Ž๐‘–๐‘— } so that if j is a prime less than or equal to L,
then ๐‘Ž๐‘–,๐‘–^๐‘— = 1. Another matrix V={๐‘ฃ๐‘–๐‘— } so that
0
๐‘ฃ0,๐‘— = {
1
๐‘–๐‘“ ๐‘— ๐‘–๐‘  ๐‘›๐‘œ๐‘ก ๐‘๐‘Ÿ๐‘–๐‘š๐‘’
๐‘–๐‘“ ๐‘— ๐‘–๐‘  ๐‘๐‘Ÿ๐‘–๐‘š๐‘’
The matrix r = V๐ด๐‘›−1
๐‘Ÿ0,๐‘— represents the number of arrangement so that the xor of the number of stones is j. So the
one we want is ๐‘Ÿ๐‘œ,๐‘œ .
In order to speed up the process, we need to use the power of 2 to do the matrix multiplication.
For example, to calculate ๐ด23 we need to calculate: ๐ด16 × ๐ด4 × ๐ด2 × ๐ด
Question F
There are two cases to consider:
๏‚ท
y√๐‘‘ is not an integer.
๏‚ท
y√๐‘‘ is an integer.
The first case is simpler as there the two roots are know:
Therefore, the equation must be: (๐‘ก −
which is:
๐‘ฅ+๐‘ฆ√๐‘‘
)(
๐‘ง
(๐‘ก −
๐‘ฅ+๐‘ฆ√๐‘‘
๐‘ง
and
๐‘ฅ−๐‘ฆ√๐‘‘
๐‘ง
๐‘ฅ−๐‘ฆ√๐‘‘
)
๐‘ง
๐‘ง 2 ๐‘ก 2 − 2๐‘ง๐‘ฅ๐‘ก + (๐‘ฅ 2 − ๐‘‘๐‘ฆ 2 ) = 0
So, the first step is to make sure that the gcd of ๐‘ง 2 , 2๐‘ง๐‘ฅ, (๐‘ฅ 2 − ๐‘‘๐‘ฆ 2 ) is 1. If not, divide each
term by the gcd.
Then, the solution is a=±p๐‘ง 2 , b==±2๐‘๐‘ง๐‘ฅ and c=±p(๐‘ฅ 2 − ๐‘‘๐‘ฆ 2 ) so that both a,b,c are between
-k,k. The number of solution is:
2๐‘˜
+1
max( ๐‘ง 2 ,2๐‘ง๐‘ฅ,(๐‘ฅ 2 −๐‘‘๐‘ฆ 2 ))
For the second case, it is more difficult. We want to illustrate with an example. Consider the
case where x=5, y=0, d=3, z=2, k=77.
So y√๐‘‘ =0 which is an integer. So, the root is: 5/2. If we put this value into the equation, we
have:
25
5
๐‘Ž+ ๐‘+c=0
4
2
or
25a+10b+4c=0
Therefore, the problem now is to find integers between -77 and 77 so that 25a+10b+4c=0.
We rearrange the equation like this:
25a+10b=-4c
Now, the gcd of 25 and 10 is 5. So the equation becomes:
5(5a+2b)=-4c
We can see that c must be a multiples of 5 and 5a+2b must be a multiple of 4.
Now, we need to find values so that 5a+2b=1. Therefore are many such solution, one of this is:
a=-1, b=2. However, since 5a+2b must be multiples of 4, therefore, the following are solutions
of the question 25a+10b+4c=0:
a=-4p, b=8p, c=5p.
If we consider the equation 5a+2b=0, we have a=-2q, b=5q.
Therefore, the general solutions is:
a=-4p-2q, b=8p+5q, c=5p
In order to find the solution, we can find the number for each of the value of p. For example,
when p=0, the solution is:
a=-2q, b=5q, c=0
So we have
-77≤-2q≤77
๏ƒž
and -77≤5q≤77
-38.5≤q≤38.5 and -15.4≤q≤15.4
๏ƒž ceiling(-15.4)≤q≤floor(15.4)
๏ƒž -15≤q≤15
So, the number of q that satisfy the above inequality is (15-(-15))+1=31.
So when c=0, the number of solutions is 31.
When c=1,
we have a=-4- 2q, b=8+5q, c=5
So we have:
-77≤-4-2q≤77
๏ƒž
and -77≤8+5q≤77
-36.5≤q≤40.5 and -13≤q≤17.8
๏ƒž ceiling(-13)≤q≤floor(17.8)
๏ƒž -13≤q≤17
The number of solution is also (17-(-13))+1=31.
So we can repeat for all the cases where p=0,±1,±2,..., ±15
This is because we also need to have -77≤5p≤77.
If we add all the number of solutions together, we get 931.
Question G
In this problem, we need a data structure to represent each grid and a data structure to
represent each row so that if one can fill up a row, he can win.
For example, in a 3x3x3 tictactao, the grids are:
(0,0,0)
(0,0,1)
...
(2,2,2)
And the rows are:
{(0,0,0), (0,0,1),(0,0,2)}
{(0,1,0), (0,1,1),(0,1,2)}
......
So a Gird class looks like this:
class Grid {
int x,y,z; // represent the grid coordinate
Vector<Row> rows; // represents the rows that contain this grid
int count; //presents the number of appearance of this grid in winning rows.
//note that there can be more than one winning rows. But all winning rows
//must meet at one grid. So there can only be one grid with count
// greater than 1.
.....
}
The Row class looks like this:
class Row {
Vector<Grid> grids; //grids associate with this row
char status// status of the grid
// ' ': not yet filled up
// 'x': up to now, only filled up with x
// 'o': up to now, only filled up with o
// '-': up to now, has been a mixture of 'x','o','-'. This means that this cannot
// be a winning row.
.......
}
So the program will fill up the corresponding rows when a mark is placed in a specific grid. We
need to check the invalid cases. A game is invalid if one of the following happens:
๏‚ท
๏‚ท
๏‚ท
๏‚ท
the difference between the number of x's and o's is greater than 1.
both x's and o's have winning rows.
the number of the winning row's marks is less than that of the looser's marks.
multiple winning rows do not meet at one grid only.
Question 1
Write a program which checks whether a string belongs the following language:
๏‚ท It contains characters a, b and c only.
๏‚ท The number of a’s in the string equals to the sum of the number of b’s and c’s in
the string.
๏‚ท For each substring of the string starting from the first characters, the number of a’s
must not be less than the number of b’s.
๏‚ท
If the string is not empty, then the first character must equal to the last one.
For example, the string “abbaca” does not belong to the language because for the
substring “abb”, which is the first 3 characters, the number of a’s is less than the number
of b’s.
On the other hand, the string “abaabcca” belongs to the language. You can see that the
number of a’s equals to the sum of the numbers of b’s and c’s. Any substring starting from
the first character contains more or equal number of a’s as compared to the number of
b’s. The first character equals to the last character.
Input
The first line of the input is the number n which specifies the number of test cases. The
line is followed by n lines so that each line contains a string to be tested.
Example
7
abbaca
baacab
dada
ababca
aaabcc
abaabcca
Output
The output for each test case is either ‘y’ or ‘n’ to specify whether the string belongs to the
language or not.
Example
n
n
y
n
y
n
y
The language is not context free and therefore cannot be parse using either LL(k) or LR(k).
To tackle this problem, we use three variables to calculate the numbers of a's, b's, and c's. So at
every point, we need to make sure that the number of a's is not less than that of b's.
Then, finally, we need to check the number of a's = the sum of numbers of b's and c's. For nonempty string, we need to check whether the first and last characters are equal.
Question 2
Write a program which calculate the value of an arithmetic expression that contains
integers, the ‘+’ , ‘*’ operators and the brackets ‘(‘ and ‘)’. Note that there is no upper limit
for the number of digits of the integers in the expressions.
The ‘+’ operator must be a binary operator. Therefore the following expressions are
invalid.
+4*4
4*(+4+8*3)
Input
The first line of the input contains an integer which represents the number of test cases.
Then it is followed by n lines for the n test cases.
Example
8
(33+404)*2+4*3
(3357323887893478759873592+1341352345234654563462)*134124644132412341
33+404)*2+4*3
+34*43
(44+44*33)*33
44*44*44+(33+44)
44+*44+44
Output
The output for a test case is either the value of the expression if it is syntactically correct.
Otherwise the output is the string ‘error’.
Example
886
450479780106920638824729770192489517283414
error
error
49368
error
85261
error
This is an LL(k) language and therefore can be parsed using parse table.
The language is:
E๏‚ฎTA
A๏‚ฎ+E
T๏‚ฎFB
B๏‚ฎ*T
F๏‚ฎ(E)|N
N๏‚ฎDC
C๏‚ฎN|๏Œ
D๏‚ฎ0|1|2|3|4|5|6|7|8|9
In the parser, the returned type for E, A, T, B,F should be BigInteger. The return type for N ,C
and D should be strings. This is because for N, C and D are used to build the bigintegers and it is
better for them to be strings.
Download