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

  1. 2
  2. 3

Sample Output

  1. 1
  2. 1

Source

 
思路详见:动态规划
java
  1. import java.util.*;
  2. import java.math.*;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) {
  7. final int maxn = 1010;
  8. BigInteger fa[] = new BigInteger[maxn];
  9. BigInteger fb[] = new BigInteger[maxn];
  10. BigInteger TWO = BigInteger.valueOf(2);
  11. fa[0] = fb[0] = BigInteger.ZERO;
  12. for (int i = 1; i < maxn; i ++) {
  13. fa[i] = fa[i-1].add(fb[i-1]);
  14. fb[i] = fa[i-1].add(fb[i-1]).add(BigInteger.valueOf(i).mod(TWO));
  15. }
  16. int n;
  17. Scanner cin = new Scanner(System.in);
  18. while (cin.hasNext()) {
  19. n = cin.nextInt();
  20. System.out.println(fb[n-1]);
  21. }
  22. }
  23. }

Python

  1. import sys
  2. fa = [0] * 1010
  3. fb = [0] * 1010
  4. fa[0] = 0;
  5. fb[0] = 0;
  6. for i in range(1, 1010):
  7. fa[i] = fa[i-1] + fb[i-1]
  8. fb[i] = fa[i-1] + fb[i-1] + i % 2
  9.  
  10. for line in sys.stdin:
  11. n = int(line)
  12. 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. Qt HUD(平显)演示程序绿色版

    把一个黑底白字的程序改成黑底绿字 上对比图,左侧是原本,右侧是仿造,有些地方比例还是有问题 其实这个程序没有啥技术含量,就是画 #include "mainwindow.h" #i ...

  2. linux下 su 与 su - 的区别和使用

    Linux下su与su -命令的区别   在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用servic ...

  3. 九度OJ--Q1167

    import java.util.Scanner;import java.util.TreeSet; /* * 题目描述: * 输入一个数组的值,求出各个值从小到大排序后的次序. * 输入: * 输入 ...

  4. ubuntu 开热点

    原文地址:https://www.cnblogs.com/king-ding/archive/2016/10/09/ubuntuWIFI.html 今天教大家一个简单方法让ubuntu发散wifi热点 ...

  5. Leetcode 672.灯泡开关II

    灯泡开关II 现有一个房间,墙上挂有 n 只已经打开的灯泡和 4 个按钮.在进行了 m 次未知操作后,你需要返回这 n 只灯泡可能有多少种不同的状态. 假设这 n 只灯泡被编号为 [1, 2, 3 . ...

  6. chrome & dark theme

    chrome & dark theme Dark Reader Extensions https://darkreader.org/help/en/ https://chrome.google ...

  7. [STL] STL各容器实现原理

    STL共有六大组件1.容器 2.算法 3.迭代器 4.仿函数 6.适配器 STL容器的实现原理 STL来管理数据十分方便,省去了我们自己构建数据结构的时间.其实,STL的实现也是基于我们常见的数据结构 ...

  8. 【bzoj4548】小奇的糖果 STL-set+树状数组

    题目描述 平面上有n个点,每个点有一种颜色.对于某一条线段,选择所有其上方或下方的点.求:在不包含所有颜色的点的前提下,选择的点数最多是多少.(本题中如果存在某颜色没有相应的点,那么选择任何线段都不算 ...

  9. 【bzoj1257】[CQOI2007]余数之和sum 数论

    题目描述 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, 3)=3 m ...

  10. 【题解】SDOI2008莎拉公主的困惑

    挺有趣的恩:洛谷P2155 在纸上打打草稿,写出n!个数,从先往后,遇到不互质的就筛掉——发现一个奇妙的性质!:筛掉的次数.顺序好像是周期性出现的呢~ 而且更加妙妙的是,好像还是m!一轮..那么因为n ...