(大数)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]);
        }
    }
}

原文地址:https://www.cnblogs.com/Weixu-Liu/p/9166488.html

时间: 2024-11-09 11:55:07

(大数)Computer Transformation hdu1041的相关文章

H - Computer Transformation(简单数学题+大数)

H - Computer Transformation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice _ Appoint description:  System Crawler  (Oct 10, 2016 1:02:59 PM) Description A sequence consisting of one digit, the numb

hdu_1041(Computer Transformation) 大数加法模板+找规律

Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8367    Accepted Submission(s): 3139 Problem Description A sequence consisting of one digit, the number 1 is initially wri

ACM学习历程—HDU1041 Computer Transformation(递推 &amp;&amp; 大数)

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, afte

HDOJ-1041 Computer Transformation(找规律+大数运算)

http://acm.hdu.edu.cn/showproblem.php?pid=1041 有一个初始只有一个1的串 每次都按①0 -> 10;②1 -> 01;这两条规则进行替换 形如:n = 1  1 n = 2  01 n = 3  1001 ... 求经过n步替换之后 串中只含复数个0的连续子串(不难发现,这种子串只能是‘00’)的出现次数 因为0<n<=1000的限制 在最坏情况下(n==1000)串的长度将达到2^1000位 排除了直接模拟上述替换过程的可能 列出前几

HDU 1041 Computer Transformation(找规律加大数乘)

主要还是找规律,然后大数相乘 #include<stdio.h> #include<string.h> #include<math.h> #include<time.h> #include<map> #include<iostream> #include<ctype.h> #include<string> #include<algorithm> #include<stdlib.h> #i

Computer Transformation(hdoj 1041)

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.

HDU 1041[Computer Transformation] 递推 高精度

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1041 题目大意:初始数字1.规则1变成01,0变成10.问经过N次完整变换后,有多少连续零对. 关键思想:考虑零对的直接来源只有"10",而"10"的直接来源只有"1"或者"00".于是可以得到以下几种递推式 1. dp[i]=pow(2,i-3)+dp[i-2];当前层多少1,下一层就多少10,下下层就多少00:当前层多少00,

Computer Transformation

题意 从1开始 1变为01 0变为10 计数变换多少次后连续0序列的个数. 题解 找规律(菲波数)+大数相加 a[i]=a[i-1]+a[i-2]*2; 1 #include <stdio.h> 2 #include<iostream> 3 #include<cstring> 4 #define N 1001 5 using namespace std; 6 char a[N][N]; 7 8 int main() 9 { 10 memset(a,'0',sizeof(

HDU 1041 Computer Transformation 数学DP题解

本题假设编程是使用DP思想直接打表就能够了. 假设是找规律就须要数学思维了. 规律就是看这些连续的0是从哪里来的. 我找到的规律是:1经过两次裂变之后就会产生一个00: 00经过两次裂变之后也会产生新的00:故此须要记录好1和00出现的次数就能够递推出后面的00出现的数据了. 公式就是tbl00[i] = tbl00[i-2] + tbl1[i-2]; 当中tbl00是记录00出现的次数,tbl1是出现1出现的次数. 公式事实上是能够化简的,只是我懒得化简了.这种公式非常清楚了. 只是因为这种数