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. Java基础之垃圾回收

    /** * 对象在没有任何引用可以到达时,生命周期结束,成为垃圾. * 所有对象在被回收之前都会自动调用finalize()方法. * ******************************** ...

  2. 星际地图制作中OB无建筑 退出问题

    星际地图制作中OB玩家没有建筑强制退出问题,目前 用下面的方法解决 ob玩家分到一个组,触发里面 延时几秒 我设置的2秒 KILL掉这个组的建筑就行~

  3. 中国 省会 地级市 经纬度 city array

    <?php $city_arr = array ( '北京' => array ( 'gis_lng' => '116.405285', 'gis_lat' => '39.90 ...

  4. 生成输出url时,使用CSS来控制超链接的格式

    在前文<生成输出url>中的第5点,介绍了使用ActionLink生成输出url中指定html标签属性. 例如, 假设Global.asax中的路由定义为: public static v ...

  5. UPPH、UPH

    UPPH=units Per Hour Per Person,单位小时人均产能,是公司作为衡量员工工作绩效的重要指标. UPPH是衡量员工单位时间工作量的一种绩效指标. UPPH计算方式如下: UPP ...

  6. Qt保证只有一个实例(将CreateMutex得到的handle通过转换得到值)

    使用CreateMutex 可以实现只启动一个应用程序实例 view plaincopy to clipboardprint?#include <QApplication>#include ...

  7. josscrowcroft / Simple-JavaScript-DOM-Inspector

    josscrowcroft / Simple-JavaScript-DOM-Inspector josscrowcroft / Simple-JavaScript-DOM-Inspector

  8. UVA-548Tree(二叉树的递归遍历)

    Tree Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Des ...

  9. HBA简介及原理

    HBA,即主机总线适配器英文“Host Bus Adapter”缩写.是一个使计算机在服务器和存储装置间提供输入/输出(I/O)处理和物理连接的电路板和/或集成电路适配器. 简介 主机总线适配器(Ho ...

  10. Android 之窗口小部件高级篇--App Widget 之 RemoteViews - 跨到对岸去

    在之前的一篇博文( Android 之窗口小部件详解--App Widge t)中,已经介绍了App Widget的基本用法和简单实例.这篇主要讲解 App Widget 的高级内容,即通过 Remo ...