Computer Transformation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7210    Accepted Submission(s): 2628

Problem 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
 
Source
 
题意:给定初始值1,然后进行变换,变换的规则是 1->01 ,0->10,问进行n次变换后串中00的个数。
 
题解:
写出前几项:
              1
             01         第1次(项)
           1       第2次(项)
       101001    第3次 (项)
01101001    第4次(项)
....
我们会发现只有当串中存在1001时会出现00

而要出现1001,则父串中要出现01

父串中出现01串有两种方法,一种是我所标注的红色部分,由祖父串中的1变过来

第二种方式就是当祖父串中有00时 父串变成 1010 时会出现 01

所以这样就可以得到递推公式:第n项00的个数 = 第 n-2项 1的个数+第n-2项 00 的个数。

f[1] = 0

f[2] = 1

f[n] = f[n-2]+2^(n-3)  ( n>2 )

import java.math.BigInteger;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
BigInteger [] h = new BigInteger[1001];
h[1] = new BigInteger("0");
h[2] = new BigInteger("1");
for(int i=3;i<=1000;i++){
h[i] = h[i-2].add(pow(i-3));
}
Scanner sc =new Scanner (System.in);
while(sc.hasNext()){
int n =sc.nextInt();
System.out.println(h[n]);
}
} private static BigInteger pow(int v) {
BigInteger sum = BigInteger.valueOf(1);
for(int i=0;i<v;i++){
sum = sum.multiply(BigInteger.valueOf(2));
}
//System.out.println(sum);
return sum;
}
}

最新文章

  1. maven增加Spring
  2. ServiceStack.OrmLite 学习笔记7-复杂点的使用1
  3. poj 3101 Astronomy (java 分数的最小公倍数 gcd)
  4. 三种不同实现初始化和销毁bean之前进行的操作的比较
  5. fido-uaf-protocol-v1.0
  6. ubuntu中mysql修改编码utf8
  7. Angular - - $sce 和 $sceDelegate
  8. Solr(五)Solr实现简单的类似百度搜索高亮功能-2代码
  9. [转载] Apache Lucene初探
  10. 20175204 张湲祯 2018-2019-2《Java程序设计》第七周学习总结
  11. android开发——Android开发中的47个小知识
  12. Vue之resource请求数据
  13. JaveScript 中的正则表达式
  14. Nginx 针对上游服务器缓存
  15. 第三个Sprint ------第三天
  16. 以CapsNet为例谈深度学习源码阅读
  17. ssm中逆向工程与分页的应用
  18. Serial Wire Debug (SWD) Interface -- PSoc5
  19. /etc/redhat-release 查看centos 版本
  20. http 相关文章

热门文章

  1. BAT大数据面试题
  2. selenium-控制浏览器操作
  3. swift的一些东西
  4. [vim]大小写转换
  5. Bigbluebutton安装过程
  6. 主机不能访问虚拟机CentOS中的站点
  7. 【设计模式】 模式PK:抽象工厂模式VS建造者模式
  8. 社会网络分析——Social Network Analysis
  9. Markdown 代码块中再内嵌一个行内代码
  10. 【洛谷 P1419】 寻找段落(二分答案,单调队列)