[luogu4285 SHOI2008] 汉诺塔 (暴力,数学)
Solution
强行猜测公式形如\(f_i=k\times f_{i-1}+b\),暴力求\(f_1,f_2,f_3\),剩下的递推就行
Code
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
inline int read() {
int x=0,f=1;char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
const int N=40;
int n;
long long f[N],top[4],t[4][N];
char ch[3];
struct M{int fr,to;}mv[10];
int get(int x) {
F(i,1,x) t[1][i]=x-i+1;
top[1]=x;top[2]=top[3]=0;
int res=0,las=0;
while(1) {
if(top[2]==x||top[3]==x) return res;
F(i,1,6) {
int u=mv[i].fr,v=mv[i].to;
if(u==las||!top[u]||(t[u][top[u]]>t[v][top[v]]&&top[v])) continue;
t[v][++top[v]]=t[u][top[u]];
t[u][top[u]--]=0;las=v;
res++;break;
}
}
}
int main() {
n=read();
F(i,1,6) scanf("%s",ch+1),mv[i].fr=ch[1]-'A'+1,mv[i].to=ch[2]-'A'+1;
f[1]=get(1);f[2]=get(2);f[3]=get(3);
int k=(f[3]-f[2])/(f[2]-f[1]),b=f[3]-f[2]*k;
F(i,4,n) f[i]=k*f[i-1]+b;
printf("%lld",f[n]);
return 0;
}
[luogu4285 SHOI2008] 汉诺塔 (暴力,数学)的更多相关文章
- 1019: [SHOI2008]汉诺塔
1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1495 Solved: 916[Submit][Status] ...
- bzoj1019 [SHOI2008]汉诺塔
1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1030 Solved: 638[Submit][Status] ...
- BZOJ 1019: [SHOI2008]汉诺塔( dp )
dp(x, y)表示第x根柱子上y个盘子移开后到哪根柱子以及花费步数..然后根据汉诺塔原理去转移... ------------------------------------------------ ...
- bzoj1019 / P4285 [SHOI2008]汉诺塔
P4285 [SHOI2008]汉诺塔 递推 题目给出了优先级,那么走法是唯一的. 我们用$0,1,2$代表$A,B,C$三个柱子 设$g[i][x]$为第$x$根柱子上的$i$个盘子,经过演变后最终 ...
- 【BZOJ1019】[SHOI2008]汉诺塔(数论,搜索)
[BZOJ1019][SHOI2008]汉诺塔(数论,搜索) 题面 BZOJ 洛谷 题解 首先汉诺塔问题的递推式我们大力猜想一下一定会是形如\(f_i=kf_{i-1}+b\)的形式. 这个鬼玩意不好 ...
- bzoj千题计划109:bzoj1019: [SHOI2008]汉诺塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1019 题目中问步骤数,没说最少 可以大胆猜测移动方案唯一 (真的是唯一但不会证) 设f[i][j] ...
- 【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)
1019: [SHOI2008]汉诺塔 Description 汉诺塔由三根柱子(分别用A B C表示)和n个大小互不相同的空心盘子组成.一开始n个盘子都摞在柱子A上,大的在下面,小的在上面,形成了一 ...
- 【bzoj1019】[SHOI2008]汉诺塔
1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1427 Solved: 872[Submit][Status] ...
- bzoj1019: [SHOI2008]汉诺塔(动态规划)
1019: [SHOI2008]汉诺塔 题目:传送门 简要题意: 和经典的汉诺塔问题区别不大,但是题目规定了一个移动时的优先级: 如果当前要从A柱子移动,但是A到C的优先级比A到B的优先级大的话,那就 ...
随机推荐
- [Tools] Using colours in a NodeJS terminal - make your output pop
Use can use colour and styles to make it easy to spot errors and group common functionality into blo ...
- HDU--1054--Strategic Game【最小点覆盖】
链接:pid=1054">http://acm.hdu.edu.cn/showproblem.php?pid=1054 题意:一个熊孩子玩策略游戏,他须要用最少的士兵守卫最多的道路.假 ...
- Buy or Build (poj 2784 最小生成树)
Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1348 Accepted: 533 Descr ...
- mysql之left join、right join、inner join的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner join(等值连接) ...
- STL_算法_逆转(reverse,reverse_copy)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e) //逆转区间数据 reverse_copy(b,e,b2) /** ...
- 将byte[]转为各种进制的字符串
/** * 将byte[]转为各种进制的字符串 * @param bytes byte[] * @param radix 基数可以转换进制的范围(2-36),从Chara ...
- 一些求数据库对象的SQL语句
use [mydb] go --存储过程 SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE routine_type='PROCEDURE' AND SP ...
- Gym - 101981G The 2018 ICPC Asia Nanjing Regional Contest G.Pyramid 找规律
题面 题意:数一个n阶三角形中,有多少个全等三角形,n<=1e9 题解:拿到题想找规律,手画开始一直数漏....,最后还是打了个表 (打表就是随便定个点为(0,0),然后(2,0),(4,0), ...
- prim解决最小生成树问题
#include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ...
- 关于EasyUI datagrid editor combogrid搜索框的实现
首先需要datagrid editor对combogrid的扩展,这个是别人实现的: $.extend($.fn.datagrid.defaults.editors, { combogrid: { i ...