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大数相加
题目 解决代码及点评 /************************************************************************/ /* ...
随机推荐
- angularJS1笔记-(14)-自定义指令(scope)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 项目复审-Bata阶段
项目复审-Bata阶段 小组的名字和链接 优点 缺点 名次 别看了你没救了 https://www.cnblogs.com/liaoyujun233/p/9148781.html 基本功能都已经实现, ...
- java杂项
简单介绍==和equals区别==是判断两个变量或实例是不是指向同一个内存空间equals是判断两个变量或实例所指向的内存空间的值是不是相同 final, finally, finalize的区别fi ...
- java 自定义异常的回顾
一.异常的分类: 1.编译时异常:编译时被检测的异常 (throw后,方法有能力处理就try-catch处理,没能力处理就必须throws).编译不通过,检查语法(其实就是throw和throws的配 ...
- 一个flume agent异常的解决过程记录
今天在使用flume agent的时候,遇到了一个异常, 现把解决的过程记录如下: 问题的背景: 我使用flume agent 来接收从storm topology发送下来的accesslog , ...
- LOJ #143. 质数判定
题目描述 判定输入的数是不是质数. 输入格式 若干行,一行一个数 x. 行数不超过 1.5×104. 输出格式 对于输入的每一行,如果 x 是质数输出一行 Y,否则输出一行 N. 样例 样例输入 ...
- Ifter Party LightOJ - 1014(水题)
题意:有C个人,给P个食物,每人吃Q个,剩L个.然后给你P和L(Q>L),让你求Q的可能情况,如果有多种可能,从小到大输出:如果不存在,输出impossible 就是求写出公式 遍历c求P-L的 ...
- [BZOJ3712]Fiolki 重构树(并查集)
3712: [PA2014]Fiolki Time Limit: 30 Sec Memory Limit: 128 MB Description 化学家吉丽想要配置一种神奇的药水来拯救世界.吉丽有n ...
- MT【161】韦恩图
(清华2017.4.29标准学术能力测试25) 若$N$的三个子集$A,B,C$满足$|A\cap B|=|B\cap C|=|C\cap A|=1$,且$A\cap B\cap C=\varnoth ...
- 【刷题】BZOJ 4825 [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...