Computer Transformation
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4548   Accepted: 1731

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 consequitive zeroes pairs that will appear in the sequence after n steps.

Sample Input

2
3

Sample Output

1
1

Source

 
思路详见:动态规划
java
import java.util.*;
import java.math.*; public class Main { public static void main(String[] args) {
final int maxn = 1010;
BigInteger fa[] = new BigInteger[maxn];
BigInteger fb[] = new BigInteger[maxn];
BigInteger TWO = BigInteger.valueOf(2);
fa[0] = fb[0] = BigInteger.ZERO;
for (int i = 1; i < maxn; i ++) {
fa[i] = fa[i-1].add(fb[i-1]);
fb[i] = fa[i-1].add(fb[i-1]).add(BigInteger.valueOf(i).mod(TWO));
}
int n;
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
n = cin.nextInt();
System.out.println(fb[n-1]);
}
}
}

Python

import sys
fa = [0] * 1010
fb = [0] * 1010
fa[0] = 0;
fb[0] = 0;
for i in range(1, 1010):
fa[i] = fa[i-1] + fb[i-1]
fb[i] = fa[i-1] + fb[i-1] + i % 2 for line in sys.stdin:
n = int(line)
print(fb[n-1])

POJ2680(动态规划,大数)的更多相关文章

  1. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

  2. 洛谷 P1005 动态规划 大数

    Problem Description 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n×m的矩阵,矩阵中的每个元素a(i,j)均为非负整数.游戏规则如下: 1 每次取数时须从每行各取走一个元素,共 ...

  3. NYOJ 541 最强的战斗力

    最强DE 战斗力 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 春秋战国时期,赵国地大物博,资源很丰富.人民安居乐业.但很多国家对它虎视眈眈.准备联合起来对赵国发起一 ...

  4. 【解题报告】[动态规划] RQNOJ - PID38 / 串的记数

    原题地址:http://www.rqnoj.cn/problem/38 解题思路: 状态表示:dp[i][j][k]表示i个A,j个B,k个C组成的满足条件的字符串的个数 初始状态:dp[0][0][ ...

  5. NI笔试——大数加法

    NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了 ...

  6. HDU1223 Order Count 动态规划 组合数

    动态规划+组合数+大数 #include<cstdio> #include<cstdlib> #include<iostream> #include<algo ...

  7. soj1166. Computer Transformat(dp + 大数相加)

    1166. Computer Transformat Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description A sequenc ...

  8. 大数问题,通常用JAVA

    e.g. HDU1002 简单加法 import java.math.BigInteger; import java.util.Scanner; public class Main { public ...

  9. [SinGuLaRiTy] 动态规划题目复习

    [SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...

随机推荐

  1. javascript中容易出bug的语句

    一.forEach方法 forEach方法中没法用continue来实现跳过单句到下一次循环,如果要跳到下一次循环,要用return关键字,如果想直接跳出forEach循环,具我所知的,要throw一 ...

  2. PostgreSQL基本配置

    记一下Postgresql的基本操作,在Ubuntu下使用apt-get安装是不会像MySQL那样都配置好了,而是要安装后再配置: 1. 基本安装 # 安装postgresql和pgadmin(一个管 ...

  3. C语言运算符(注意事项)

    1.C语言取余注意事项:%   a.求余.模运算符(%)时要求两数必须是整型数据. b.取余的结果,是取决于被除数   (不管除数是正数 还是 负数,模的符号与被除数的符号相同).   例:8÷2=4 ...

  4. spring mvc:实现给Controller函数传入map参数

    [1]前端js调用示例: ...fillOrDiffer?inMapJson={"2016-08-31 0:00:00":0.1,"2016-08-31 0:15:00& ...

  5. 完整Android开发基础入门博客专栏

    博客地址:http://www.runoob.com/w3cnote/android-tutorial-contents.html

  6. 软件工程项目组Z.XML会议记录 2013/09/18

    软件工程项目组Z.XML会议记录 [例会时间]2013年9月18日周三21:00-23:00 [例会形式]小组讨论 [例会地点]三号公寓楼会客厅 [例会主持]李孟 [会议记录]毛宇 会议整体流程 一. ...

  7. 个人作业4——alpha阶段个人总结(201521123103 吴雅娟)

    一.个人总结 在alpha 结束之后, 每位同学写一篇个人博客, 总结自己的alpha 过程: 请用自我评价表:http://www.cnblogs.com/xinz/p/3852177.html 有 ...

  8. 利用vue-cli搭建项目后的目录结构

    npm install -g vue-cli vue init webpack my-project(项目名称)  后的目录结构: -----build         webpack配置相关 --- ...

  9. Spark中如何生成Avro文件

    研究spark的目的之一就是要取代MR,目前我司MR的一个典型应用场景即为生成Avro文件,然后加载到HIVE表里,所以如何在Spark中生成Avro文件,就是必然之路了. 我本人由于对java不熟, ...

  10. [C/C++] #ifdef和#endif

    一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是“条件编译”.有时,希望当满足某条件时对一组语句进行编译,而当条 ...