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 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 a1a2, …, an (1 ≤ a1a2, …, 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

分析:

学习网址:http://blog.csdn.net/acm18810549519/article/details/10155281

动态规划

题意:汉诺塔问题,不过其中加了一个条件,就是盘子可以有相同的

可以发现,当移动第i 种盘子时&& num[i]>=2得到的是逆序的,但是题目要求不能改变顺序,所以必须处理

1.先考虑不按顺序的情况为 dp1[i]=dp1[i-1]*2+num[i]  ,num[i]为相同盘子的个数

公式得来:如果从A到C轴,借助B轴,i种盘子,要先把i - 1种移动到 B, 需要dp1[i - 1],然后把第i种移动到C,需要num[i],然后再把i - 1种从B移动到C,需要dp1[i - 1]
所以得到dp1[i]=dp1[i-1]*2+num[i] .

2.考虑按顺序的情况为 dp2[i]=2*dp1[i-1]+2*num[i]+dp2[i-1]

公式得来:把num[1] - 1个移动到辅助轴,这时这num[1] - 1个盘子的顺序颠倒了,然后把第num[1]中最后一个移动到目标轴,然后把辅助轴上的移动回来,再次颠倒,恢复顺序,得到dp2[1] = 2 * (num[1] - 1) +1。
对于dp2[i],
如果x[i] == 1,那么第i种就不需要考虑顺序(只有一种),所以dp2[i] = dp1[i]
否则,第i种要调动2次,保证顺序不变。
还是从A到C轴,借助B轴,i种盘子
把i - 1种不考虑顺序移到C,需要dp1[i - 1].
把第i种从A移动到B,需要num[i](颠倒顺序),
把i - 1种不考虑顺序移到A(腾出C),需要dp1[i - 1].
把第i种从B移动到C,需要num[i](顺序恢复)
把i - 1种考虑顺序移到C,需要dp2[i - 1].
所以有dp2[i] = 2 * dp1[i - 1] +2 * num[i] +dp2[i - 1]
最后答案是dp2[n].

 //起始条件:dp1[1]=num[1];dp2[1]=2*num[1]-1
//num[i]==1:dp2[i]=2*dp1[i-1]+1
//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]
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
int num[],dp1[],dp2[];
int main(){
int n,m;
while(cin>>n>>m){
int i=;
for(;i<=n;i++){
cin>>num[i];
}
dp1[]=num[]%m;
dp2[]=(*(num[]-)+)%m;
for(i=;i<=n;i++){
dp1[i]=(*dp1[i-]+num[i])%m;
if(num[i]==){
dp2[i]=dp1[i];
}
else{
dp2[i]=(*dp1[i-]+*num[i]+dp2[i-])%m;
}
}
cout<<dp2[n]<<endl;
}
return ;
}

poj 3601 Tower of Hanoi的更多相关文章

  1. 尺取法 POJ 3601 Subsequence

    题目传送门 /* 题意:求连续子序列的和不小于s的长度的最小值 尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案 1. 记录前i项的总和,求[i, p)长度的最小值,用二分找到sum[p] ...

  2. python递归三战:Sierpinski Triangle、Tower of Hanoi、Maze Exploring

    本文已做成视频教程投稿b站(视频版相对文本版有一些改进),点击观看视频教程 本文主要通过三个实例来帮助大家理解递归(其展示动画已上传B站): 谢尔宾斯基三角形(Sierpinski Triangle) ...

  3. 汉诺塔问题(The Tower of Hanoi)的递归算法与非递归算法

    非递归算法: 根据圆盘的数量确定柱子的排放顺序: 若n为偶数,按顺时针方向依次摆放 A B C: 若n为奇数,按顺时针方向依次摆放 A C B. 然后进行如下操作: (1)按顺时针方向把圆盘1从现在的 ...

  4. poj 1920 Towers of Hanoi

    Towers of Hanoi Time Limit: 3000MS   Memory Limit: 16000K Total Submissions: 2213   Accepted: 986 Ca ...

  5. Tower of Hanoi问题

    [问题描述] 有A, B, C三个塔座,A上套有n个直径不同的圆 盘,按直径从小到大叠放,形如宝塔,编号1, 2, 3 … n. 要求将n个圆盘从A移到C,叠放顺序不变,移动过程中遵循 下列原则: w ...

  6. [POJ1958][Strange Tower of Hanoi]

    题目描述 求解 \(n\) 个盘子 \(4\) 座塔的 Hanoi 问题最少需要多少步 问题分析 考虑 \(3\) 座塔的 Hanoi 问题,记 \(f[i]\) 表示最少需要多少步, 则 \(f[i ...

  7. One usage of recurison: the tower of Hanoi

    Statements: This blog was written by me, but most of content  is quoted from book[Data Structure wit ...

  8. 汉诺塔 Tower of Hanoi

    假设柱子标为A,B.C.要由A搬至C,在仅仅有一个盘子时,就将它直接搬至C:当有两个盘子,就将B作为辅助柱.假设盘数超过2个.将第二个下面的盘子遮起来,就非常easy了.每次处理两个盘子,也就是:A- ...

  9. codeforces 392B Tower of Hanoi

    把前n个碟子从第一个塔移动到第三个塔有两种方法: 1.把前n-1个移动到第二个塔,把第n个移动到第三个塔,然后把前n-1个从第二个移动到第三个: 2.把前n-1个移动到第三个塔,把第n个移动到第二个塔 ...

随机推荐

  1. TunnelBroker for EdgeRouter 后记

    最近入手了UBNT EdgeRouter X, 想着用 IPv6在路由上FQ,经过两天折腾,终于正常使用,留下点标记 供后来同学借鉴. TUNNEL的注册和配置,可以完全按这篇文章来: TunnelB ...

  2. 20145218张晓涵 web安全基础实践

    20145218张晓涵 web安全基础实践 1.实验后回答问题 SQL注入攻击原理,如何防御 SQL注入漏洞是指在Web应用对后台数据库查询语句处理存在的安全漏洞.也就是,在输入字符串中嵌入SQL指令 ...

  3. mysql按照天或小时group分组统计

    select DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * ) FROM test GROUP BY DATE_FORMAT( ...

  4. 数据库架构设计的三种模式:share nothing , share everythong , share disk

    数据库构架设计中主要有Shared Everthting.Shared Nothing.和Shared Disk: Shared Everthting:一般是针对单个主机,完全透明共享CPU/MEMO ...

  5. 神器与经典--sp_helpIndex

    ======================================================= 每每和那些NB的人学习技术的时候,往往都佩服他们对各个知识点都熟捻于心,更佩服的是可以在 ...

  6. MFC学习(三):项目学习

    1. 概述 MFC程序由CWinApp.MainFrm(含Menu,可用CSplitterWndEx分割).众多Dialog等组成. MFC既可以使用纯Dialog的形式,也可以使用Document+ ...

  7. ASP.NET Web API总结

    1. 跨域 提供Http层的web api时,通常需要考虑跨域问题. 因为浏览器处于安全考虑,默认不允许前端页面向不是自己所在的ip/域名发起请求,因此需要服务器端指明自己允许部分或所有域名进行跨域请 ...

  8. [转]解读Unity中的CG编写Shader系列7——漫反射

    如果前面几个系列文章的内容过于冗长缺乏趣味着实见谅,由于时间原因前面的混合部分还没有写完,等以后再补充,现在开始关于反射的内容了.折射与反射在物理世界中,光的反射与折射往往是同时存在的,光源由真空或者 ...

  9. 整理javascript基础

    原始类型值(Primitive values) 下面的全是原始类型值(简称:原始值): 布尔类型:true,false 数字类型:1736,1.351 字符串类型: 'abc',"abc&q ...

  10. php中的list()用法中要注意的地方

    php中list()函数是用数组对一列值进行赋值, 该函数只用于数字索引的数组,且假定数字索引从0开始.(这句话很重要,是从索引0开始为变量赋值,如果对应的数字索引不存在,则对应位的变量也为空值.) ...