poj 3601Tower of Hanoi
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 1895 | Accepted: 646 |
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 conical shape. The objective of the puzzle is to move the entire stack to another peg, obeying the following rules:
- Only one disk may be moved at a time.
- Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
- No disk may be placed on top of a smaller disk.
For n disks, it is 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 are mutually distinguishable. Their ordering at the beginning should be preserved at the end, though it may be disturbed during the process of solving the puzzle.
Given the number of disks of each size, compute the number of moves that the optimal solution takes.
Input
The input contains multiple test cases. Each test case consists of two lines. The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 106). The second lines contains n integers a1, a2, …, an (1 ≤ a1, a2, …, an ≤ 105). For each 1 ≤ i ≤ n, there are ai disks of size i. The input ends where EOF is met.
Output
For each test case, print the answer modulo m on a separate line.
Sample Input
1 1000
2
5 1000
1 1 1 1 1
5 1000
2 2 2 2 2
5 1000
1 2 1 2 1
Sample Output
3
31
123
41
Source
解题报告:
假设有n种盘子,x[i]为第i种盘子的数目, 0 <= i <= n - 1.
我们先计算出相同的盘子不考虑顺寻的情况,记作a[i],表示有i种盘子所需的步骤数目(不考虑第i种顺序)。
容易知道,a[0] = x[0],只有一种时,直接把这种的所有盘子移动到目标轴上。
a[i] = 2 * a[i - 1] x[i].
说明:如果从A到C轴,借助B轴,i种盘子,要先把i - 1种移动到 B, 需要a[i - 1],然后把第i种移动到C,需要x[i],然后再把i - 1种从B移动到C,需要a[i - 1]
所以得到a[i] = 2 * a[i - 1] x[i].
但是题目是需要考虑相同盘子的顺序的,这里记作b[i],为移动i种盘子考虑顺序需要的步骤。
b[0] = 2 * (x[0] - 1) 1.
说明:把x[0] - 1个移动到辅助轴,这时这x[0] - 1个盘子的顺序颠倒了,然后把第x[0]中最后一个移动到目标轴,然后把辅助轴上的移动回来,再次颠倒,恢复顺序,得到b[0] = 2 * (x[0] - 1) 1。
对于b[i],
如果x[i] == 1,那么第i种就不需要考虑顺序(只有一种),所以b[i] = a[i]
否则,第i种要调动2次,保证顺序不变。
还是从A到C轴,借助B轴,i种盘子
把i - 1种不考虑顺序移到C,需要a[i - 1].
把第i种从A移动到B,需要x[i](颠倒顺序),
把i - 1种不考虑顺序移到A(腾出C),需要a[i - 1].
把第i种从B移动到C,需要x[i](顺序恢复)
把i - 1种考虑顺序移到C,需要b[i - 1].
所以有b[i] = 2 * a[i - 1] 2 * x[i] b[i - 1]
最后答案是b[n - 1].
题意:Hanoi塔问题,只不过有一些盘子的大小是一样的,所有的盘子转移到另一个塔,相同大小的盘子要保证原本的顺序不变,问你最少的步骤数目,结果对m取余。
输入n,m,n表示有几种盘子大小,m表示结果将要取余的数。
第二行输入n个数,表示盘子从小到大每种盘子大小的个数。如{1,3,2}表示大小为1的盘子有1个,大小为2的盘子有3个,大小为3的盘子有2个。
附上代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int i,j,n,m,a[],b[],x[];
while(~scanf("%d%d",&n,&m))
{
for(i=; i<n; i++)
scanf("%d",&x[i]);
a[]=x[];
for(i=; i<n; i++)
a[i]=(a[i-]*+x[i])%m;
b[]=*x[]-;
for(i=; i<n; i++)
{
if(x[i]==)
b[i]=a[i];
else
b[i]=(*a[i-]+*x[i]+b[i-])%m;
}
printf("%d\n",b[n-]);
}
return ;
}
poj 3601Tower of Hanoi的更多相关文章
- poj 3601 Tower of Hanoi
Tower of Hanoi Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 1853 Accepted: 635 De ...
- POJ 1958 Strange Towers of Hanoi 解题报告
Strange Towers of Hanoi 大体意思是要求\(n\)盘4的的hanoi tower问题. 总所周知,\(n\)盘3塔有递推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i ...
- POJ 1958 Strange Towers of Hanoi
Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3784 Accepted: 23 ...
- 【POJ 1958】 Strange Towers of Hanoi
[题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...
- poj 3572 Hanoi Tower
Hanoi Towers Time Limit : 10000/5000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- poj 1920 Towers of Hanoi
Towers of Hanoi Time Limit: 3000MS Memory Limit: 16000K Total Submissions: 2213 Accepted: 986 Ca ...
- Strange Towers of Hanoi POJ - 1958(递推)
题意:就是让你求出4个塔的汉诺塔的最小移动步数,(1 <= n <= 12) 那么我们知道3个塔的汉诺塔问题的解为:d[n] = 2*d[n-1] + 1 ,可以解释为把n-1个圆盘移动到 ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
随机推荐
- webpack学习之—— Code Spliting(代码分离)
代码分离特性能够把代码分离到不同的 bundle 中,然后可以按需加载或并行加载这些文件.代码分离可以用于获取更小的 bundle,以及控制资源加载优先级,如果使用合理,会极大影响加载时间. 有三种常 ...
- SpringBoot 使用 @Value 从 YAML文件读取属性
在 YAML中有如下配置 paypal: mode:live 在类中,通过 @Value属性读取 @Value("${paypal.mode}") private String m ...
- poj3532 Round Numbers
题意:给出l.r,求区间[l,r]内二进制中0的个数大于等于1的个数的数字有多少个. 简单的数位dp. //Serene #include<algorithm> #include< ...
- swiper踩过的哪些坑
最近,苦恼于各种轮播的需求,每个自己都要自己写,写的挺烦的.终于,在网上发现了swiper插件,发现还是挺实用的,但其中还是踩过了不少的坑,其中有不少都是很简单的问题,但到发现的时候都是花了不少时间, ...
- C++中String字符串查找
在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,对于在C中,我们经常用到strstr()或者strchr()这两种方法.而对于C++的string,我们往往会用到find(). C ...
- 【JZOJ1637】【ZJOI2009】狼和羊的故事
题目描述 "狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......" Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干 ...
- SDUT-2107_图的深度遍历
数据结构实验之图论二:图的深度遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请定一个无向图,顶点编号从0到n-1 ...
- Java SDUT-2562_相似三角形
相似三角形 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给出两个三角形的三条边,判断是否相似. Input 多组数据 ...
- 条件变量用例--解锁与signal的顺序问题
我们知道,当调用signal/broadcast唤醒等待条件变量的其他线程时,既可以在加锁的情况下调用signal/broadcast,也可以在解锁的情况下调用. 那么,到底哪种情况更好呢?man手册 ...
- HLSL效果框架
原文:HLSL效果框架 HLSL效果框架能简化许多操作.这里先不写具体的效果框架的程序,在处理多光源光照的时候再整理. 下一章:效果框架-多种光源的多光源 叠加效果 这儿先列出效果框架的一个注意点: ...