Day 6 Zhonghao Xi
Classic Title: stone merger
Piles may be combined in any consideration of the number of exclusive OR (^) and
F [s] s the binary merge into a pile of the stone corresponds to the cost of the flowers
Enumeration of the subsets s
#include<iostream> using namespace std; int main() { cin >> n; for (int a=0;a<n;a++) cin >> z[a]; for (int a=0;a<(1<<n);a++) for (int b=0;b<n;b++) if (a & (1<<b)) sum[a] += z[b]; memset(f,0x3f,sizeof(f)); for (int a=0;a<n;a++) f[1<<a] = 0; for (int s=0;s<(1<<n);s++) for (int a=1;a<s;a++) if ( (s|a) == s) f[s] = min(f[s],f[a]+f[a^s]+(sum[a]^sum[a^s])); for (int s=0;s<(1<<n);s++) for (int a=(s-1)&s;a;a=(a-1)&s) f[s] = min(f[s],f[a]+f[a^s]+(sum[a]^sum[a^s])); cout << f[(1<<n)-1] << endl; }
And today's topic is the game theory [dp nim game : It is said that 80% of game theory dp eventually be transformed into nim game up]
In fact, this type of exercise related only to Alice and Bob (fog
This thing is probably this
There is a game g, Alice and Bob are two people in Van it is a turn-based game that no tie, the outcome of conditions: when a person can not be operated lose
Below are many, many questions to illustrate
T1
Have an integer S (2 <= S <= 200), the upper hand minus a number x in S, is at least 1, but less than S. After the two sides take turns to S subtract a positive integer, but can not exceed k times the number of the previous round each other to lose, the party reduced to 0 wins. He asked whether the upper hand to win?
solution:
From one state to another state, and the state will be lost win state occurs
f [s] s-> false must be reported s-> true win
State by the s-> s1.s2.s3 ······
If for any f [si] = true is to win the state.
Direct memory search!
dp [i] [j] represents the number of the remaining current is i, the number is reduced up to state j, dp [i] [j] = = 1 win otherwise losing
dp[i][j]->dp[i-x][x*k](1<=x<=j)
If for some x, dp [ix] [x * k] = = 0, then dp [i] [j] = 1, otherwise dp [i] [j] = 0
Requirements for dp [S] [S-1]
O(S^3)
f [i] [j] left i, an opponent is reduced number j, this time he is winning or losing state [state] pseudocode
#include<iostream> using namespace std; bool f[][],g[][]; bool dfs(int i,int j) { if (i==0) return false; if (g[i][j]) return f[i][j]; g[i][j]=true;f[i][j]=false; for (int r=1;r<=i && r<=k*j;r++) if (dfs(i-r,r) == false) f[i][j]=true; return f[i][j]; } int main() { cin >> s >> k; for (int a=1;a<s;a++) if (dfs(s-a,a) == false) { cout << "Alice" << endl; return 0; } cout << "Bob" << endl; return 0; }
T2
If there are n games
Sg defined function: the minimum number of functions in no state can be transferred to sg
sg[0]=0
sg theorem:
sg value of n for each game sg = XOR value up
T3
Now if you can only take away 1-4 stones
The same operation: sg [0] = 0 sg [1] = 1 sg [2] = 2 sg [3] = 3 sg [4] = 4 sg [5] = 0
T4
stones piled n + 1, the leftmost pile of stones there 2012, two people are operating. Operator can select a different heap piles by increasing or decreasing a stone (minus one plus one, or to have no stones left plus a heap are allowed). In order to ensure that the game will end in a finite number of steps, the provisions of piles selected in the right side of the pile must contain an odd number of stones, who lost no way out. Asked whether the upper hand to win?
N parity calculation stack, the stack stones all odd subscript exclusive or taken out, to see whether the 0
There are piles of stones as a concept and do not
By transforming the odd heap of stones, to solve the problem
The leftmost pile heap regarded as 0, the number of stones pile of stones odd numbered XOR together, the result is 0 losing the upper hand, or else win
In fact, the game is converted to nim
T5
There u N N stones piled on stairs, stairs number 0..N-1, each stack has a [n] A stone. They take turns to play each time an arbitrary heap of stones to move to any number on that flight of stairs below it, the stone number 0 can not move. Until all the stones are moved to No. 0, that person win. Just get asked whether there is a winning strategy.
XOR up the number of steps from the end of an odd number of pieces of the grid is the answer
T6
u1xN (1 <= N <= 2000) the sub-spaces, both operated in turn, each selected from a grid is not labeled, it is marked, if the person after operation, there are 3 consecutive grid are marked, then he would win, and asked whether the upper hand has a winning strategy?
The game as a game more
Subdivided into multiple games
sg [i], for a length of the horizontal bar sg is the number i
Prevent cross-border: behind the array plus a large enough number on the line
#include<iostream> #include<algorithm> #include<vector> using namespace std; int dfs(int n) { if (n==0) return 0; if (f[n]) return sg[n]; f[n]=true; vector<int> z; for (int a=1;a<=n;a++) { int l = max(a-3,0); int r = max(n-a-2,0); z.push_back( dfs(l) ^ dfs(r) ); } sort(z.begin(),z.end()); z.push_back(233333333); for (int a=0,p=0;;a++) { if (z[p] != a) { sg[n] = a; return sg[n]; } while (z[p]==a) p++; } } int main() { cin >> n; if (dfs(n)) cout << "Alice" << endl; else cout << "Bob" << endl; }
Leave a practice:
bzoj2798
Day 7 Zhonghao Xi
Establishment of fire stations
Dp not practice
N<=100000
f[i][0/1][k]
In themselves, father, brother, grandfather nodes can be covered
(Obviously put the best 4 nodes)
For the first tree to find out the depth of the deepest node, find their grandfather, grandfather put a soldier at the junction, to cover his grandfather to delete nodes, and so on, for the rest of the tree similar operations until the tree was all covered
Maintenance on the heap, the time complexity of O (n log n)
Obtaining k factors of n
M ^ k can recalculation
If people are numbered, seeking different groups do?
Each group n / r individuals, the number of programs
So many cases, and simplification
After using Euler's theorem to calculate the conversion
1e9-401 is a prime, and 1e9-402 = 2 * 13 * 5281 * 7283
Let n factorial% 21,352,817,283
Recombined
% Because the number is small, there will be a section cycle appears
f [i] [j] represents a step away i, j come
N number of points, a little right
There are m times asked each time to the two points p1, p2, on this pathway will find the three points, so that they may form a triangular
m,n<=1e6
Fibonacci number
If the distance between two points is greater than 42, the output yes
If less, violence can
bzoj tree triangle