Zoj 1039 number game (State compression, memory-based search)

Source: Internet
Author: User

Reprinted please indicate the source, thank youHttp://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: 2-20 this 19-digit game. Each time a number is taken away, the number multiples cannot be obtained, and the sum of two numbers that cannot be obtained.

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 39

Because there are only 19 data records, you can perform a memory search by compressing and storing the status. DP [1 <19]. DP [I] indicates that a viable set of I is a must-win situation.

Next, state transfer is important.

For the original state, if you want to remove the number X from the set. There are two steps:

1. Remove the multiples of x and X. This is to perform a bit operation at a specific position of a number 0. For more information, seeCode

2. Remove the sum of elements not in the set and multiples of X. This step can be implemented in reverse order to find the elements in the set and subtract several X numbers from them to determine whether the new number is in the set. If not, this number is invalid, remove. It can also be solved through bitwise operations

In winning and losing, the N-State must follow the p-state.

Finally, we need to output a feasible solution, which is to enumerate and delete a certain number and determine whether to change to the P state.

# Include <iostream> # include <cstdio> # include <ctime> # include <cstring> # include <cmath> # include <algorithm> # include <cstdlib> # include <vector> # Define c 240 # define time 10 # define INF 1 <25 # define ll long longusing namespace STD; int DP [1 <19]; int get_state (INT state, int X) {int ret = State; // remove the multiples of x and X. // first, set the location to 1, and then reverse the value. Finally, it matches the state phase with for (INT I = X; I <= 20; I + = x) RET & = ~ (1 <(I-2); For (INT I = 2; I <= 20; I ++) {// If a number is still in the set, then determine whether there is a number not in the Set and the multiples of X constitute if (1 <(I-2) & RET) {for (Int J = x; i-j-2> = 0; J + = x) // If I is the sum of a number not in the set and X, if (! (1 <(i-j-2) & RET) {RET & = ~ (1 <(I-2); break ;}} return ret ;}int get_dp (INT state) {If (DP [State]! =-1) return DP [State]; for (INT I = 2; I <= 20; I ++) {If (State & (1 <(I-2 ))) {// remove the number of I from the set int TMP = get_state (state, I); // P State to obtain the N state if (! Get_dp (TMP) return DP [State] = 1 ;}} return DP [State] = 0 ;}int main () {memset (DP,-1, sizeof (DP); DP [0] = 0; int t, n, a [20], CAS = 0; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); int state = 0; For (INT I = 0; I <n; I ++) {scanf ("% d", & A [I]); // initial state | = 1 <(A [I]-2 );} printf ("Scenario # % d: \ n", ++ CAS); If (get_dp (state) = 0) puts ("There is no winning move. \ n "); else {printf (" the winning moves are: "); For (INT I = 0; I <n; I + +) {// Enumerative removal, whether it is a mandatory defeat state int TMP = get_state (State, a [I]); If (! Get_dp (TMP) printf ("% d", a [I]);} puts (". \ n") ;}} 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.