Ultraviolet A 1378-a funny stone game (game)

Source: Internet
Author: User

Ultraviolet A 1378-a funny stone game

Question Link

Given n piles of stones, I, J, K, and 3 stacks (I <j <= k) can be selected each time, and then a bunch of functions will be collected from I, stack one to the other two and fail to get the final result. Ask if the first hand can win. If yes, output the first three stacks selected.

Idea: in a combined game, it needs to be converted. All the stones should be lined up in a single line. N stacks cannot be obtained. Therefore, assume that each stone represents a pile, from left to right are n-1, n-2, N-3... 2, 1, 0, and then take one and add two at a time, it is equivalent to taking out a pile and adding two more heaps, which is converted to the NIM problem. Then, for each pile of stones, you only need to consider his parity, because if it is an even number, the latter hand can be the same as the first hand, to ensure that the situation will not change, only the odd number will change, therefore, when seeking for the game and the game at the beginning, we only consider the odd number of stacks, and finally enumerate the three locations (in fact, this step is a little complicated). Then, after the result is obtained, the NIM sum is 0, which means the game wins, if you cannot find a winner, you will lose.

Code:

# Include <cstdio> # include <cstring> const int n = 30; int n, a [n], SG [N], vis [N * n]; void getsg () {for (INT I = 0; I <23; I ++) {memset (VIS, 0, sizeof (VIS); For (Int J = 0; j <I; j ++) {for (int K = J; k <I; k ++) {vis [SG [J] ^ SG [k] = 1 ;}} for (Int J = 0; j ++) if (! Vis [J]) {SG [I] = J; break ;}} void solve () {int sum = 0; For (INT I = 0; I <N; I ++) {scanf ("% d", & A [I]); if (a [I] & 1) sum ^ = SG [n-I-1];} For (INT I = 0; I <n; I ++) {If (! A [I]) continue; For (Int J = I + 1; j <n; j ++) {for (int K = J; k <n; k ++) {If (sum ^ SG [n-I-1] ^ SG [n-J-1] ^ SG [n-k-1]) = 0) {printf ("% d \ n", I, j, k); Return ;}}}} printf ("-1-1-1 \ n");} int main () {int CAS = 0; getsg (); While (~ Scanf ("% d", & N) {printf ("game % d:", ++ CAS); solve () ;}return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.