"bzoj3105" "cqoi2013" "New Nim Game" "Linear base + greedy"

Source: Internet
Author: User

DescriptionThe traditional NIM game is this: there are some match stacks, each with several matches (the number of matches in different heaps can be different). Two players take turns, each time you can choose a match heap to remove several matches. You can take one or the whole bunch of matches, but not from more than a dozen matches at the same time. The player who took the last match won. the game is slightly different: in the first round, the first player can take a number of matches directly from the whole pile. Can not take a bunch of, but can not take all. The second round is also the same, the second player also has such an opportunity. Starting with the third turn (and the first player), the rules are the same as the Nim game. If you take it first, how can you guarantee to win? If you can win, let the first round match the total number of matches as small as possible.  InputThe first behavior of the integer K. That is, the number of match stacks. The second line contains a positive integer of not more than 109 K , that is, the number of matches in each heap.  Outputoutputs the minimum number of matches taken in the first round. If there is no guarantee to win, output-1. Sample Input6
5 5 6 6 5 5
Sample Output +HINT

k<=100

The problem: The prerequisite for winning is that there is no XOR or 0 subset of the remaining matches. so we need to look for the great linear Independent group, the answer is the sum minus the weights of the maximal linear independent group and. Can prove that this is a quasi-array, and then greedy just fine. The greedy process maintains a linear base.

#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;int ins[50],k,a[1001], C[1001];long long Ans,sum;int Main () {scanf ("%d", &k), for (int i=1;i<=k;i++) scanf ("%d", &a[i]); Sort (a+1,a+k +1); for (int i=1;i<=k;i++) sum+= (Long Long) (C[i]=a[i]), for (int. i=k;i>=1;i--)  {for   (int j=30;~j;j--)      if (a[i]& (1<<j))       {          if (!ins[j])           {             ins[j]=i;break;           }          else A[i]^=a[ins[j]];       } if (A[i]) ans+= (long Long) c[i];  } printf ("%lld", Sum-ans);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"bzoj3105" "cqoi2013" "New Nim Game" "Linear base + greedy"

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.