POJ1351 Number of Locks(数学)
截至写博客为止,貌似这是网上第一个采用数学公式来处理的。
网上的题解都是DFS或是动态规划,但感觉可以推公式直接用数学的方法处理,想了好久,终于推出公式。
题意:一个长度为n的由数字1,2,3,4 组成的序列,求至少有一对1,4相邻且2或3必须用上的方法数。
思路: 计A为有1,4相邻的方法数,B为有1,4相邻且无2,3的方法数,则答案为A - B
B很容易求,为2 ^ n - 2 ,再考虑A
计f(n)为有1,4相邻的方法数,g(n)为无1,4相邻但以1,或4开头的方法数
长度为n - 1且有1,4相邻的序列,后面接上1,2,3,4,仍然有1,4相邻,无1,4相邻但以1,4开头前面加上1,4 ,变成有1,4相邻的序列,故有递推公式
f[n] = 4 * f[n - 1 ] + g[n - 1]
考虑g(n) ,1或4后面是2,3,方法数为2 * 2 *(4 ^(n - 2) - f(n - 2)),或者1或4后面数与他相同,则变成一个字问题,方法数为g(n - 1),故
g(n) = 2 * 2 *(4 ^(n - 2) - f(n - 2)) + g(n - 1)
故可采取递推方式
代码如下:
1 #include<cstdio>
2 #include<iostream>
3 #include<cstdlib>
4 #include<cstring>
5 #include<string>
6 #include<algorithm>
7 #include<map>
8 #include<queue>
9 #include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = , INF = 0x3F3F3F3F;
LL f[N], g[N];
int main(){
f[] = ;
f[] = ;
g[] = ;
g[] = ;
for(int i = ; i <= ; i++){
f[i] = * f[i - ] + g[i - ];
g[i] = ( <<( * i - )) - * f[i - ] + g[i - ];
}
int n;
while(~scanf("%d", &n) && n != -){
printf("%d: %I64d\n",n, f[n] - ( << n) + );
}
return ;
31 }
采用数学方法,代码如此简洁
POJ1351 Number of Locks(数学)的更多相关文章
- MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE
今天进行MySql 一个表数据的清理,经过漫长等待后出现 The total number of locks exceeds the lock table size 提示.以为是table_cache ...
- mysql:The total number of locks exceeds the lock table size
使用mysql InnoDB存储引擎进行大量数据的更新,删除的时候容易引发”The total number of locks exceeds the lock table size”问题,解决方法之 ...
- MySQL配置文件路径及‘The total number of locks exceeds the lock table size’问题
在删除mysql中的数据时,遇到报错: ERROR 1206 (HY000): The total number of locks exceeds the lock table size 查了查,发现 ...
- mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
1. 问题背景 InnoDB是新版MySQL(v5.5及以后)默认的存储引擎,之前版本的默认引擎为MyISAM,因此,低于5.5版本的mysql配置文件.my.cnf中,关于InnoD ...
- Mysql_解决The total number of locks exceeds the lock table size错误
在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number o ...
- MySQL解决[Err] 1206 - The total number of locks exceeds the lock table size问题
MySQL解决[Err] 1206 - The total number of locks exceeds the lock table size问题 查看MySQL版本:mysql>show ...
- mysql 数据库缓存调优之解决The total number of locks exceeds the lock table size错误
环境: mysql5.6.2 主从同步(备注:需操作主库和从库) 一.InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_poo ...
- 【MySQL笔记】mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
step1:查看 1.1 Mysql命令行里输入"show engines:"查看innoddb数据引擎状态, 1.2 show variables "%_buffer% ...
- MySQL插入大批量数据时报错“The total number of locks exceeds the lock table size”的解决办法
事情的原因是:我执行了一个load into语句的SQL将一个很大的文件导入到我的MySQL数据库中,执行了一段时间后报错"The total number of locks exceeds ...
随机推荐
- 6 HandlerDescriptor 处理程序描述类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- struts2和hibernate整合的小Demo
jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> ...
- Extjs String转Json
var jsonStr= '{ "name": "zhangsan", "age": "18" }'; var json ...
- XAMPP端口占用启动不了
skype默认会占用80和443端口 如果在apache之前启动skype,apache就会启动不了了!! 解决办法很简单: 1. 先启动apache再启动skype,这样skype就会换其他的端口监 ...
- VirtualBox中安装Ubuntu12.04/Ubuntu14.04虚拟机
NOTE: 一开始安装的Ubuntu12.04,后来又重新安装了14.04.截图基本使用了安装12.04时的截图,后来安装14.04时又补充了几张.该安装过程对Ubuntu12.04和14.04都是适 ...
- js隐藏div和class
<style type="text/css"> //div用点//class# .footer { display:none; } #footer { displ ...
- Python模块之optparse
参考: http://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html https://docs.python.org/2/li ...
- flume与kafka整合
flume与kafka整合 前提: flume安装和测试通过,可参考:http://www.cnblogs.com/rwxwsblog/p/5800300.html kafka安装和测试通过,可参考: ...
- PHP中文字符串编码转换
2016年2月26日 16:47:13 星期五 情景: PHP从csv导入数据时乱码 $name = mb_convert_encoding($name, 'UTF-8', 'ASCII,GBK,GB ...
- java压缩
/* @description:压缩文件操作 * @param filePath 要压缩的文件路径 * @param descDir 压缩文件保存的路径 d:\\aaa.zip */ public s ...