Tower of Hanoi
| Time Limit: 1000MS |
|
Memory Limit: 131072K |
| Total Submissions: 1853 |
|
Accepted: 635 |
Description
The Tower of Hanoi is a puzzle consisting of three pegs and a number of disks of different sizes which can slide onto any Peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conica L shape. The objective of the puzzle is to move the entire stacks to another peg, obeying the following rules:
- Only one disk is moved at a time.
- Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, in top of the other disk s, may already is present on the that peg.
- No disk is placed on top of a smaller disk.
For n disks, it's a well-known result that the optimal solution takes 2n −1 moves.
To complicate the puzzle a little, we allow multiple disks to be of the same size. Moreover, equisized disks is mutually distinguishable. Their ordering at the beginning should is preserved at the end, though it could be disturbed during the process of solving t He puzzle.
Given the number of disks of each size, compute the number of moves the optimal solution takes.
Input
The input contains multiple test cases. Each test case consists of lines. The first line contains the integers n and m (1≤ n ≤ 100, 1≤ m ≤106). The second lines contains n integers a1, a2, ..., an ( 1≤ a1, a2, ..., C10>an ≤105). For each 1≤ i ≤ n, there is ai disks of size I. The input ends where EOF is met.
Output
For each test case, print the answer modulo m in a separate line.
Sample Input
1 100025 10001 1 1 1 15 10002 2 2 2 25 10001 2 1 2 1
Sample Output
33112341
Source
PKU Campus (POJ founder Monthly contest–2008.05.10), XFXYJWF
Analysis:
Study site: http://blog.csdn.net/acm18810549519/article/details/10155281
Dynamic planning
Test instructions: Hanoi tower problem, but one of the conditions is that the plate can have the same
It can be found that when you move the first plate I && num[i]>=2 get the reverse order, but the problem can not change the sequence, so must deal with
1. First consider the case of Dp1[i]=dp1[i-1]*2+num[i], Num[i] is the number of the same plate
Formula obtained: If from A to C axis, with the help of B-axis, I kind of plate, to first move the i-1 species to B, need dp1[i-1], and then move the type I to C, need num[i], and then the i-1 species from B to C, need dp1[i-1]
So get dp1[i]=dp1[i-1]*2+num[i].
2. Consider the order of the case as Dp2[i]=2*dp1[i-1]+2*num[i]+dp2[i-1]
Formula obtained: Move num[1]-1 to the auxiliary axis, then this num[1]-1 plates in the order of reversal, and then the last one in the num[1] to the target axis, and then the auxiliary axis to move back, reverse again, restore order, get dp2[1] = 2 * (Num[1] 1) + 1.
For Dp2[i],
If x[i] = = 1, then I do not need to consider the order (only one), so dp2[i] = Dp1[i]
Otherwise, the first type I will be transferred 2 times, to ensure that the order is unchanged.
Or from A to C axis, with the B-axis, I kind of plate
The i-1 is moved to C without regard to the order of dp1[i-1].
Move the type I from A to B, need num[i] (reverse order),
Move I-1 to a (free C), need dp1[i-1].
Move the type I from B to C, need num[i] (sequential recovery)
Move the i-1 into C, need dp2[i-1].
So there is dp2[i] = 2 * dp1[i-1] +2 * num[i] +dp2[i-1]
The final answer is Dp2[n].
1 //starting Condition: dp1[1]=num[1];DP 2[1]=2*num[1]-12 //num[i]==1:dp2[i]=2*dp1[i-1]+13 //Num[i]>=2:dp2[i]=dp1[i]+num[i]+dp1[i]+num[i]+dp2[i]=2*dp1[i-1]+2*num[i]+dp2[i-1]4#include <cstdio>5#include <cmath>6#include <cstring>7#include <iostream>8 using namespacestd;9 intnum[ the],dp1[ the],dp2[ the];Ten intMain () { One intn,m; A while(cin>>n>>m) { - intI=1; - for(; i<=n;i++){ theCin>>Num[i]; - } -dp1[1]=num[1]%m; -dp2[1]=(2* (num[1]-1)+1)%m; + for(i=2; i<=n;i++){ -Dp1[i]= (2*dp1[i-1]+num[i])%m; + if(num[i]==1){ Adp2[i]=Dp1[i]; at } - Else{ -Dp2[i]= (2*dp1[i-1]+2*num[i]+dp2[i-1])%m; - } - } -cout<<dp2[n]<<Endl; in } - return 0; to}
POJ 3601 Tower of Hanoi