1166. Computer Transformat

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

本来还以为是一道基本的动态规划题目,结果做了一遍WA,然后仔细观察,到了1000时数太大了,long long都放不下,所以尝试大数相加,一次就过了。。

思路:

dp[i][0] —— 第i轮后出现多少个01

dp[i][1] —— 第i轮后出现多少个1

dp[0][0] = 0; dp[0][1] = 1;

dp[1][0] = dp[1][1] = 1;

dp[i][0] = dp[i-2][0] + dp[i-1][1];

dp[i][1] = 2*dp[i-1][1];

n = dp[n-1][0];

#include <iostream>
#include <string>
using namespace std; string dp[1001][2]; string add(string a,string b)
{
string result;
string rr;
int i;
int l1,l2,len1,len2;
l1 = len1 = a.size();
l2 = len2 = b.size();
int aa,bb,cc,dd;
dd = 0;
while(l1 > 0 && l2 > 0)
{
aa = a[l1-1] - '0';
bb = b[l2-1] - '0';
cc = (aa + bb+dd) % 10;
dd = (aa + bb+dd) / 10;
result += cc+'0';
l1--;
l2--;
}
while(l1 > 0)
{
aa = a[l1-1] - '0';
cc = (aa + dd) % 10;
dd = (aa + dd) / 10;
result += cc + '0';
l1--;
}
while(l2 > 0)
{
bb = b[l2-1] - '0';
cc = (bb + dd) % 10;
dd = (bb + dd) / 10;
result += cc + '0';
l2--;
}
if(dd == 1)
result += '1';
for(i = result.size() - 1;i >= 0 ;i--)
rr += result[i];
return rr;
} void init()
{
int i;
dp[0][0] = "0";
dp[0][1] = "1";
dp[1][0] = dp[1][1] = "1";
for(i = 2;i <= 1000;i++)
{
dp[i][0] = add(dp[i-2][0],dp[i-1][1]);
dp[i][1] = add(dp[i-1][1],dp[i-1][1]);
}
} int main()
{
int n;
init();
while(cin >> n)
{
cout << dp[n-1][0] << endl;
}
return 0;
}

soj1166. Computer Transformat(dp + 大数相加)的更多相关文章

  1. hdu acm-1047 Integer Inquiry(大数相加)

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  3. 随机数组&大数相加

    随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中 一,      设计思路: 先生成随机数数组,再将数组保存在一个字符串中,然后将数组各数字加和, ...

  4. java-两个大数相加

    题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...

  5. POJ 1503 Integer Inquiry(大数相加,java)

    题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...

  6. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  7. Linux C/C++ 编程练手 --- 大数相加和大数相乘

    最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...

  8. hdu1002大数相加

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. 基于visual Studio2013解决C语言竞赛题之1077大数相加

        题目 解决代码及点评 /************************************************************************/ /* ...

随机推荐

  1. angularJS1笔记-(1)-多控制器

    前端写好: <div ng-app="myApp"> <div ng-controller="firstController"> < ...

  2. windows多线程(八) 信号量Semaphore

    如果你看到了这里,我就认为你已经对掌握了有关关键段 CriticalSection.互斥量Mutex和事件Event有关的内容,所以最基本的东西就不再介绍了.如果没有掌握上面说的内容,可以看这里: 关 ...

  3. dstat 监控时,无颜色显示

    linux:Centos 6.6 dstat:0.7.0 注意,有这个提醒:Color support is disabled, python-curses is not installed good ...

  4. L2 L3 L4

    第二层交换机,是根据第二层数据链路层的MAC地址和通过站表选择路由来完成端到端的数据交换的.因为站表的建立与维护是由交换机自动完成,而路由器又是属于第三层设备,其寻址过程是根据IP地址寻址和通过路由表 ...

  5. html select options & vue h render

    html select options & vue h render https://developer.mozilla.org/en-US/docs/Web/HTML/Element/opt ...

  6. filebeat向kafka中传输数据报WARN Failed to connect to broker DOMSDev07:9092: dial tcp: lookup DOMSDev07: getaddrinfow: No such host is known.解决方法

    打开filebeat客户端所在机器C:\Windows\System32\drivers\etc目录,找到hosts文件 以记事本形式打开,在底部追加 “IP 主机名” 即可

  7. 一个不错的微信应用JS API库

    1.API能实现什么? 1.分享到微信朋友圈 2.分享给微信好友 3.分享到腾讯微博 4.新的分享接口,包含朋友圈.好友.微博的分享(for iOS) 5.隐藏/显示右上角的菜单入口 6.隐藏/显示底 ...

  8. requestAnimationFrame 优雅降级

    if (!Date.now) Date.now = function() { return new Date().getTime(); }; (function() { 'use strict'; v ...

  9. hihocoder1639 图书馆 [数学]

    已知数组a[]及其和sum, 求sum! / (a1!a2!...an!) 的个位数的值. 求某数的逆元表写成了求某数阶乘的逆元表,故一直没找到错误. P 是质数的幂B 表示质数,P 表示模数,cal ...

  10. 洛谷 P1199 三国游戏 解题报告

    P1199 三国游戏 题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有\(N\)位武将(\(N\)为 ...