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大数相加
题目 解决代码及点评 /************************************************************************/ /* ...
随机推荐
- Source Insight中的多行注释
转自:http://www.cnblogs.com/dongzhiquan/archive/2013/03/04/2943448.html 我们经常要对一整段代码进行注释,很多代码编辑器都提供了这样的 ...
- KEIL C51 printf格式化输出特殊用法
作者:dragoniye 发布:2014-02-15 12:44 分类:硬件 抢沙发 /*******************************************KEI ...
- 关于2013年1月21日的DNS故障分析文章
首页 资讯 小组 资源 注册 登录 首页 最新文章 业界 开发 IT技术 设计 创业 IT职场 访谈 在国外 经典回顾 更多 > - 导航条 - 首页 最新文章 业界 - Googl ...
- NULL,"",String.Empty三者在C#中的区别
(1)NULLnull 关键字是表示不引用任何对象的空引用的文字值.null 是引用类型变量的默认值.那么也只有引用型的变量可以为NULL,如果int i=null,的话,是不可以的,因为Int是值类 ...
- 半夜思考之查漏补缺, Spring 的 Bean 后处理器
有一篇写的是容器后处理器, 这篇是 Bean 后处理器 , 我对这个 Bean 后处理器的理解就是一个 AOP 编程 . Bean 后处理器 : 是一种特殊的 Bean , 这种 Bean 不对外提供 ...
- 函数防抖与函数节流 封装好的debounce和throttle函数
/** * 空闲控制 返回函数连续调用时,空闲时间必须大于或等于 wait,func 才会执行 * * @param {function} func 传入函数,最后一个参数是额外增加的this对象,. ...
- python的/和//运算
#谁能告诉我这个框框怎么去掉!!! python中"/"操作为除法操作,"//"操作为整数除操作,具体差异如下 " / "表示 浮点数除法, ...
- git 复位出现If no other git process is currently running, this probably means a git process crashed in this repo
复位到A节点的时候点了取消(终止),又去复位另外个节点,结果每次不管复位哪个都会报这个错误 fatal: Unable to create 'XXXXXXXXX' : File exists. If ...
- 批量更新 A表的PK_ID字段
UPDATE ASET PK_ID=(SELECT ID FROM B WHERE A.TAB_NAME=B.TAB_NAME AND B.IS_KEY='1' ) AB表 以TAB_NAME 做 ...
- 【NOI2016】区间
目链接:http://uoj.ac/problem/222 在数轴上有 n 个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m 个区间共同包含至少 ...