Problem Description
A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step,
the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on. 



How many pairs of consequitive zeroes will appear in the sequence after n steps? 
 
Input
Every input line contains one natural number n (0 < n ≤1000).
 
Output
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
 
Sample Input
2
3
 
Sample Output
1
1

找规律的问题,分开0和1的衍生问题就好理解了。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std; const short MAX_N = 1001;
vector<short> tbl0[MAX_N], tbl1[MAX_N];//inverse saved numbers void addLargeNum(vector<short> &rs, vector<short> &a, vector<short> &b)
{
int n = (int)a.size(), m = (int)b.size();
rs.clear();
short carry = 0;
for (int i = 0, j = 0; i < n || j < m || carry; i++, j++)
{
short an = i < n ? a[i] : 0;
short bn = j < m ? b[i] : 0;
carry += an + bn;
rs.push_back(carry % 10);
carry /= 10;
}
} void plusOne(vector<short> &a)
{
if (a.empty()) a.push_back(1);
else
{
int i = 0;
while (i < (int)a.size() && 9 == a[i]) a[i++] = 0;
if (i == (int)a.size()) a.push_back(1);
else a[i]++;
}
} void getTbl()
{
tbl0[0].push_back(0); tbl0[1].push_back(0);
tbl1[0].push_back(0); tbl1[1].push_back(0); for (int i = 2; i < MAX_N; i++)
{
addLargeNum(tbl0[i], tbl1[i-1], tbl0[i-1]);
addLargeNum(tbl1[i], tbl0[i-1], tbl1[i-1]);
if (!(i&1)) plusOne(tbl1[i]);
}
} int main()
{
getTbl();
int n;
while (scanf("%d", &n) != EOF)
{
vector<short> &a = tbl1[n];
short m = (short)tbl1[n].size();
for (short i = m-1; i >= 0; i--)
{
printf("%d", a[i]);
}
putchar('\n');
}
return 0;
}

HDU Computer Transformation1041 题解的更多相关文章

  1. 致初学者(一): HDU 2000~ 2013题解

    对于开始学习C语言程序设计或C++程序设计面向过程部分的同学来说,利用在线OJ网站进行实践训练,对提高自己的编程能力很有好处.国内外OJ网站很多,每个都去看看,去刷个题,是不现实的,也没必要.即使一个 ...

  2. 爆零后的感受外加一道强联通分量HDU 4635的题解

    今天又爆零了,又是又,怎么又是又,爆零爆多了,又也就经常挂嘴边了,看到这句话,你一定很想说一句””,弱菜被骂傻,也很正常啦. 如果你不开心,可以考虑往下看. 翻到E(HDU 4635 Strongly ...

  3. 致初学者(二): HDU 2014~ 2032题解

    下面继续给出HDU 2014~2032的AC程序,供大家参考.2014~2032这19道题就被归结为“C语言程序设计练习(三) ”~“C语言程序设计练习(五) ”. HDU 2014:青年歌手大奖赛_ ...

  4. 致初学者(三): HDU 2033~ 2043题解

    下面继续给出HDU 2033~2043的AC程序,供大家参考.2033~2043这10道题就被归结为“ACM程序设计期末考试(2006/06/07) ”和“2005实验班短学期考试 ”. HDU 20 ...

  5. HDU 6583 Typewriter 题解

    ——本题来自杭电多校第一场 题意:给定一个字符串,主角需要用打字机将字符串打出来,每次可以: 1.花费p来打出任意一个字符 2.花费q来将已经打出的某一段(子串)复制到后面去 对于这种最优化的问题,我 ...

  6. HDU 5961 传递 题解

    题目 我们称一个有向图G是 传递的,当且仅当对任意三个不同的顶点a,,若G中有 一条边从a到b且有一条边从b到c ,则G中同样有一条边从a到c. 我们称图G是一个 竞赛图,当且仅当它是一个有向图且它的 ...

  7. HDU 5963 朋友 题解

    题目 B君在围观一群男生和一群女生玩游戏,具体来说游戏是这样的: 给出一棵n个节点的树,这棵树的每条边有一个权值,这个权值只可能是0或1. 在一局游戏开始时,会确定一个节点作为根.接下来从女生开始,双 ...

  8. 「POJ3436」ACM Computer Factory题解

    题意: 有很多台机器,可以把物件从一种状态改装成另一种状态,初始全为\(0\),最终状态全为\(1\),让你可以拼凑机器,请问最大总性能为多少,且要求输出方案. 题解: 这道题是真的水啊,我不想写太多 ...

  9. HDU 1106 排序 题解

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

随机推荐

  1. ROS服务的理解

    服务是节点之间通信的另一种方式,服务允许节点发起一个请求和接收一个响应. 打开终端在里面输入: roscore 查看当前的运行节点: rosnode list 返回结果: /rosout 查看当前的运 ...

  2. String字符串需要掌握的几个问题

    一.两种定义方式的区别: String str = "hello";      先在堆内存中查找是否已经有"hello",若有,将str指向已存在的它即可:若堆 ...

  3. win7系统下连接使用mac 蓝牙键盘(Apple Wireless Keyborad)

    这几天买了一个apple wireless keyborad 玩玩,主要是给孩子买了一个ipad 搭配上wireless keyborad让她玩app足够了,就当一部电脑用吧. 看起来挺精致的,可以了 ...

  4. 为IE6-7间接支持:before和:after伪类

    :before和:after我们经常会用到,特别是在做移动端页面时,利用它制作文字前后的ICON.图片的垂直居中之类的非常方便且代码简洁(当然,功能远比这些要多的多...). 可是在PC端,由于现在还 ...

  5. 页面的拼装配置Appache SSI

    O(∩_∩)O哈哈哈~今天又遇到了一个好东西,记录一下下哈~ 一个网站,有些页面的头部跟尾部是相同的,还在为总是复制,为错了一个小小点而需要改好多个页面烦恼吗?现在我知道怎么来偷懒了! 1.首先找到c ...

  6. 记录.net 中的常见术语

    --Entity Framework和NHibernate --EF和NH都是一种ORM技术.就是对象关系模型映射. --NHibernate和Entity Framework 4.0优劣势争论 -- ...

  7. Address already in use: JVM_Bind错误的解决

    1,独立运行的Tomcat没有关闭. 自安装的tomcat程序设置开机自动运行,或者在之前运行过,先关闭ecplipse或jbuilder,在任务管理器中找到Tomcat的进程,将其 kill掉,即可 ...

  8. IBM HeapAnalyzer

    https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/W3b463571efc8_4f02_99af_3cbc0 ...

  9. hdu 5461 Largest Point

    Thinking about it: 对于式子 a * ti * ti + b * tj,可以看作时有两部分构成 a * ti * ti 和 b * tj,如果整个式子要最大,则要求这两部分都要尽量大 ...

  10. mysql的索引问题

    注意:索引一般适合用于经常查询的数据,可以提高查询效率:但是不适合用于经常用到增.删.改的数据:会影响效率低. 1.unique key->(唯一索引)在一张表里可以有多个,起到约束的作用:避免 ...