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. iOS- 什么是GitHub?关于它的自我介绍「初识 GitHub」

    1 前言 我一直认为 GitHub 是程序员必备技能,程序员应该没有不知道 GitHub 的才对,我当初接触 GitHub 也大概工作了一年多才开始学习使用,我读者里很多是初学者,而且还有很多是在校大 ...

  2. 转载---Atom编辑器常用快捷键

    常用快捷键–亲测及翻译 英文 中文 快捷键 功能 New Window 新建界面窗口 Ctrl + Shift + N 如中文意思 New File 新建文件 Ctrl + N 如中文意思 Open ...

  3. Python入门:字符串的分片与索引、字符串的方法

    这是关于Python的第3篇文章,主要介绍下字符串的分片与索引.字符串的方法. 字符串的分片与索引: 字符串可以用过string[X]来分片与索引.分片,简言之,就是从字符串总拿出一部分,储存在另一个 ...

  4. Longest Substring with At Most Two Distinct

    Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...

  5. linux中创建和解压文档的 tar 命令教程

    linux & zip & tar https://www.cnblogs.com/xgqfrms/p/9714161.html 1 linux中的tar命令 tar(磁带归档)命令是 ...

  6. 【Linux笔记】在后台执行scp,实现服务器间无密码文件拷贝。

    远程备份大容量时常会有这样的情形:从远程备份的文件很大,需要很长时间,想在退出ssh后程序依然能继续在后台下载,可以通过建立服务器间安全信息关系和nohup的方式解决. 有两台服务器:A服务器IP 1 ...

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

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

  8. C# Socket模拟发送接收

    Socket简介 通过TCP/IP与仪器或设备通讯,在C#语言中,我们通常采用Socket.本项目是一个简单的Socket建立服务监听与Socket作为客户端请求的一个示例. 项目结构 客户端项目 S ...

  9. Eclipse中设置新创建文件的默认编码格式

    window-prefenences-web-jsp(或者是其他文件格式,里面是一个列表) 找到之后点击,在右侧区域中选择encoding进行修改即可,然后应用,OK

  10. The Bells are Ringing UVALive - 4060(枚举求解)

    输出整数N,使得  t1 <= N  统计有多少组t1,t2,t3,满足:1<t1<t2<t3<=1000000,t3-t1<=25,且t1,t2,t3的最小公倍数 ...