zoukankan      html  css  js  c++  java
  • (大数)Computer Transformation hdu1041

    Computer Transformation

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    Total Submission(s): 8688    Accepted Submission(s): 3282

    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

    java,递推

    递推:0->10  ;

             1->01;

             00->1010;

             10->0110;

              01->1001;

              11->0101;

    假设a[i]表示第i 步时候的00的个数,由上面的可以看到,00是由01 得到的,所以只要知道a[i-1]的01的个数就能够知道a[i]的00的个数了,那a[i-1]怎么求呢,同样看推导,01由1和00 得到,而第i步1的个数是2^(i-1),所以a[i]=2^(i-3)+a[i-2];(最后计算的是第i-2步情况)。

    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            BigInteger a[]=new BigInteger[1001];
            while(in.hasNextInt()) {
                int n=in.nextInt();
                a[1]=BigInteger.valueOf(0);
                a[2]=BigInteger.valueOf(1);
                a[3]=BigInteger.valueOf(1);
                for(int i=4;i<=n;i++) {
                    a[i]=BigInteger.valueOf(0);      //先进行初始化。
                    int m=i-3;        //在大数的pow(m,n)中,n是int类型的,m是BigInteger类型的。             
                    BigInteger q= new BigInteger("2");
                    a[i]=a[i].add(q.pow(m));
                    a[i]=a[i].add(a[i-2]);
                }
                System.out.println(a[n]);
            }
        }
    }
    View Code
  • 相关阅读:
    应用环境配置记录
    【C#】Dictionary通过value获取对应的key值
    DevExpress 之 GridControl 自定义列(转)
    C#中gridView常用属性和技巧介绍(转)
    【643】cv2.imread() 函数
    【642】Python 实现膨胀、腐蚀、提取边线
    【639】keras 中 fit_generator 的 数据生成器
    【638】keras 多输出模型【实战】
    【637】一个图片两个标注的图像增强
    别自嗨了!想做新生代农民工,你还不够格。。
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/9166488.html
Copyright © 2011-2022 走看看