截至写博客为止,貌似这是网上第一个采用数学公式来处理的。

网上的题解都是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(数学)的更多相关文章

  1. MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE

    今天进行MySql 一个表数据的清理,经过漫长等待后出现 The total number of locks exceeds the lock table size 提示.以为是table_cache ...

  2. mysql:The total number of locks exceeds the lock table size

    使用mysql InnoDB存储引擎进行大量数据的更新,删除的时候容易引发”The total number of locks exceeds the lock table size”问题,解决方法之 ...

  3. 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 查了查,发现 ...

  4. 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 ...

  5. Mysql_解决The total number of locks exceeds the lock table size错误

    在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number o ...

  6. 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 ...

  7. mysql 数据库缓存调优之解决The total number of locks exceeds the lock table size错误

    环境: mysql5.6.2  主从同步(备注:需操作主库和从库) 一.InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_poo ...

  8. 【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% ...

  9. MySQL插入大批量数据时报错“The total number of locks exceeds the lock table size”的解决办法

    事情的原因是:我执行了一个load into语句的SQL将一个很大的文件导入到我的MySQL数据库中,执行了一段时间后报错"The total number of locks exceeds ...

随机推荐

  1. 获取客户端IP

    function getIP(){ $ip = ""; if (getenv("HTTP_CLIENT_IP") && strcasecmp(g ...

  2. 5.3---找最近的两个数(CC150)

    public static int[] getCloseNumber(int x){ int[] res = new int[2]; int i = 1; int num = oneNumber(x) ...

  3. phpcms更换域名||外网访问本地网站

    在网站在发展的过程中,很可能多次的修改域名:然后就发现我的搜索页还是之前的域名的链接地址,那么在PHPCMS V9中我们要怎么进行设置呢?   请进行以下步骤的修改: 修改/caches/config ...

  4. 一个很详细的web.xml讲解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...

  5. wireshark 和 Httpwatch tcpdump

    wireshark 功能强大,适用性高.过滤功能好. Httpwatch 功能单一,优缺点明显,但是非常适合抓取http交互的包,而且可以非常明确的显示出整个的交互过程. tcpdump linux ...

  6. SIP介绍

    1.概述: SIP(Session Initiation Protocol,会话初始协议)是由IETF制定的多媒体通信协议.它是一个基于文本的应用层控制协议,用于创建.修改和释放一个或多个参与者的会话 ...

  7. BestCoder20 1002.lines (hdu 5124) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...

  8. 用window.print()打印指定div里面的内容(转载的)

    用window.print()打印指定div里面的内容 今天客户让添加个打印证照功能,直接用window.print()打印的是整个页面,而用以下方法就可以只打印证明了 <!--window.p ...

  9. vs2010:fatal error LNK1123: 转换到 COFF 期间失败

    解决方法: 项目\属性\配置属性\清单工具\输入和输出\嵌入清单:原来是“是”,改成“否”.

  10. 【python】id()函数

    来源:百度知道 >>> a=2.0 >>> b=2.0 >>> id(a) 524440880 >>> id(b) 524440 ...