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 ;
}

最新文章

  1. 网络--&gt;监控--&gt;OID--&gt;BGP
  2. android中将EditText改成不可编辑的状态
  3. jspSmartUpload上传下载全攻略
  4. Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
  5. PHP+MYSQL会员系统的开发实例教程
  6. PCB工艺镀金(电金)和沉金(化金)的区别
  7. 如何获取数据块结构信息dump
  8. 微信读书 iOS 性能优化总结
  9. Ehcache(2.9.x) - API Developer Guide, Cache Usage Patterns
  10. 【JSP】让HTML和JSP页面不缓存从Web服务器上重新获取页面
  11. Python 2.X-关于函数返回的数值类型
  12. 【Beta】Daily Scrum Meeting——Day7
  13. 深入理解Java虚拟机读书笔记8----Java内存模型与线程
  14. python学习笔记:深浅拷贝的使用和原理
  15. jmeter 中 Client implementation HttpClient4和java区别实践一
  16. 使用 node 创建代码服务器
  17. 2019.1.23 DFMEA for
  18. 微信小程序填坑之路
  19. ArcGIS API 和GIServer
  20. Oracle表字段的增删改和重命名

热门文章

  1. robotframework+jenkins分布式执行自动化测试用例
  2. js常用的校验代码 (整理)
  3. OpenGL学习脚印:背面剔除(Face Culling)
  4. 理解图像Garbor和HOG特征的提取方法及实例应用
  5. 比较旧的写法:验证车牌、手机号、电话、qq等
  6. bzoj4241: 历史研究(回滚莫队)
  7. mysqldump导出数据不带时区信息的问题
  8. 小记一次shellscript的麻烦
  9. Linux 下安装 resync 介绍
  10. 追随自己的价值观:用研经理 Anne Diaz 职业探索之路