soj1166. Computer Transformat(dp + 大数相加)
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 + 大数相加)的更多相关文章
- hdu acm-1047 Integer Inquiry(大数相加)
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 用字符串模拟两个大数相加——java实现
问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...
- 随机数组&大数相加
随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中 一, 设计思路: 先生成随机数数组,再将数组保存在一个字符串中,然后将数组各数字加和, ...
- java-两个大数相加
题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...
- POJ 1503 Integer Inquiry(大数相加,java)
题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- Linux C/C++ 编程练手 --- 大数相加和大数相乘
最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...
- hdu1002大数相加
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 基于visual Studio2013解决C语言竞赛题之1077大数相加
题目 解决代码及点评 /************************************************************************/ /* ...
随机推荐
- keil c51笔记
第一章 Keil C51开发系统基本知识 第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性. ...
- ASP.NET MVC 1.0 参考源码索引
http://www.projky.com/asp.netmvc/1.0/System/Web/Mvc/AcceptVerbsAttribute.cs.htmlhttp://www.projky.co ...
- 父元素如果为none,子元素也是看不到的
1.最近遇到一个问题,就是获取一个子元素的offsetwidth 的值总是为0 .原因是因为把父元素给设置成none了. 2.给元素赋值宽高 div.style.width=330+'px' 要加上p ...
- jmeter 多线程组间变量共享
jmeter的线程组之间是相互独立的,各个线程组互不影响,所以线程组A中输出的参数,是无法直接在线程组B中被调用的. 但是有时为了方便管理,我们可能是把各个接口单独存放在不同的线程组中.拿Cookie ...
- Windows 10 正式版原版ISO镜像
Win10正式版32位简体中文版(含家庭版.专业版)文件名: cn_windows_10_multiple_editions_x86_dvd_6846431.isoSHA1:21B824F402927 ...
- linux c 判断文件存在,遍历文件,随机修改文件内容
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<assert.h> #in ...
- Spring(2):Spring Ioc
1.下载spring-framework-3.2.0 完整包下载路径: https://repo.spring.io/webapp/#/artifacts/browse/tree/Properties ...
- Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用
前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...
- 【刷题】BZOJ 1926 [Sdoi2010]粟粟的书架
Description 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Cormen 的文章.粟粟家中有一个 R行C 列的巨型书架,书 ...
- BZOJ 3224 普通平衡树 | 平衡树模板
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...