hdu 1041(递推,大数)
2016-04-08 14:52
302 查看
Computer Transformation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7210 Accepted Submission(s): 2628
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
Every input line contains one natural number n (0 < n ≤1000).
[align=left]Output[/align]
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
[align=left]Sample Input[/align]
2
3
[align=left]Sample Output[/align]
1
1
[align=left]Source[/align]
Southeastern Europe 2005
题意:给定初始值1,然后进行变换,变换的规则是 1->01 ,0->10,问进行n次变换后串中00的个数。
题解:
写出前几项:
1
01 第1次(项)
1001 第2次(项)
01101001 第3次 (项)
1001011001101001 第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
= 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 ); } } 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; } }
相关文章推荐
- 选择字段,创建用户自定义的水晶报表
- 管道命令
- 网页设计Web尺寸规范
- IOS 触摸事件、手势识别、摇晃事件、耳机线控
- 【python学习】re模块——正则表达式
- Linux服务器需要安装的基本软件
- linux静态链接库与动态链接库详解
- linux程序同时链接动态库与静态库
- C语言中数组的一些基本知识小结
- React Native 安装 Android 运行环境
- mycat-web 配置
- iOS开发之触摸事件以及手势
- HTTP协议小解
- Matlab 实例分析
- ThinkPHP 3.1.2 查询方式的一般使用2
- 踩气球ZOJ1003
- Mysql支持的数据类型(总结)
- 《python计算机视觉编程》读书笔记------9(Scipy篇)
- 每个极客都应该知道的Linux技巧
- APP代码规范