"bzoj-2460&3105" Element & New NIM Game Dynamic Maintenance linear base + greedy

Source: Internet
Author: User

3105: [cqoi2013] New Nim game time limit:10 Sec Memory limit:128 MB
submit:839 solved:490
[Submit] [Status] [Discuss] Description The 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. Input first behavior integer k。 That is, the number of match stacks. The second line contains kA positive integer not exceeding 109, that is, the number of matches in each heap. Output outputs 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

Source 2460: [BeiJing2011] element time limit:20 Sec Memory limit:128 MB
submit:564 solved:304
[Submit] [Status] [Discuss] Description

Legend has it that, in ancient times, on the Western continent of magic land, people have mastered the use of magic ore refining staff technology. It was then realized that the mana of a scepter depended on the ores used. In general, the more ore the more powerful, but extremes meet: Sometimes, people in order to get stronger mana and
The use of a lot of ore, but in the refining process found that the magic ore all disappeared, so as not to refine the staff, this phenomenon is called "magic Offset."  In particular, if more than one piece of ore is used in the refining process, a "magic offset" is bound to occur. Later, with the improvement of people's cognitive level, this phenomenon has been well explained. After a large number of experiments, the famous mage Dmitri found: If you give a reasonable number of each ore found now (number is a positive integer, called the ore element ordinal), then, an ore combination will produce a "magic offset" when and only if there is a non-empty set, The elements of those ores are either bitwise XOR or up to zero. (If you're not sure what the XOR is, see the next page for a noun explanation.)   For example, using two of the same ores will inevitably occur as "magic offsets" because the two ores have the same sequence of elements, which are zero or different. And people have an effective way of measuring magic, already know: The magic of the crafting staff is equal to the Falizhi of each kind of ore.    The mana of all the ores found today has been measured, and the ordinal number of each ore is calculated by experiment. Now, given your ore information, you can calculate how much magic the staff at that time could have been refining.

Input

The first line contains a positive integer n, which represents the number of species of ore.
The next n lines, two positive integers per line, numberi and Magici, represent the element ordinal and mana value of the ore.

Output

Package only one line, an integer: The maximum mana value

Sample Input3
1 10
2 20
3 30
Sample Output50
HINT

As a result of the "magic offset" the fact that each ore is used up to one piece.
If all three ores are used, because the element sequence of the three is different or up: 1 XOR 2 xor 3 = 0,
There will be magic offsets and no staves.
It can be found that the best option is to select the latter two ores, with Mana of 20+30=50.

For all data: N≤1000,numberi≤10^18,magici≤10^4.

Source

Day2

Solution

Linear base, dynamic maintenance + greedy

Greedy to sort, according to the influence of the answer from the big to the small sort, and then dynamically maintain the linear base, determine the XOR and whether it is 0

Seemingly greedy or something to prove = = (play an egg)

Code
BZOJ2460
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Long LongRead () {Long Longx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 1010intN,ans;structysnode{Long LongNum;intMag; BOOL operator< (ConstYsnode & A)Const {returnMag>A.mag;}} A[MAXN];Long Long Base[MAXN];intMain () {N=read (); for(intI=1; i<=n; i++) A[i]. Num=read (), A[i]. mag=read (); Sort (a+1, a+n+1); for(intI=1; i<=n; i++) { for(intj= -; j>=0; j--) if((a[i). NUM&GT;&GT;J) &1) { if(!Base[j]) {Base[J]=i; Break;} ElseA[i]. num^=a[Base[j]]. Num; } if(A[i]. Num) ans+=A[i]. Mag; } printf ("%d\n", ans); return 0;}

"bzoj-2460&3105" Element & New NIM Game Dynamic Maintenance 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.