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

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

题目大意就是有0-> 10, 1->01这两种变换,起始状态是1,让求某次变换后连续两个0的个数,此处有且仅两个0连续。

首先枚举前几项的话

0:1

1:01

2:1001

3:01101001

4:1001011001101001

5:01101001100101101001011001101001

……

发现两个个规律,

1:

所有项前一半和后一半互反,此外,偶数项对称。

想来也是,只要满足某个前一项前一半和后一半互反,0->10,1->01后,自然造成后一项同样互反的形式。

同样的前一个偶数项的对称会导致后面奇数项的前一半和后一半都对称,自然导致后一个偶数项对称。

此处严谨证明应由数学归纳法。

2:

后一项等于前一项取反拼上前一项。

发现奇数项由于前后互反,在交接处会形成01,那么下一项在对称情况下,交界处必形成1001。所以偶数项的0对会多生成一个。

有了这些就知道了两项间的关系。

于是由2得:

zero[i] = zero[i-1]+one[i-1]+(i-1)%2;

one[i] = zero[i-1]+one[i-1];

由于递推式看起来增长速度就和费波拉契有的一比,估计需要大数。实测确实需要。

代码:

import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        BigInteger one[] = new BigInteger[1005];
        BigInteger zero[] = new BigInteger[1005];
        one[1] = new BigInteger("0");
        one[2] = new BigInteger("0");
        zero[1] = new BigInteger("0");
        zero[2] = new BigInteger("1");
        for (int i = 3; i <= 1000; ++i)
        {
            zero[i] = zero[i-1].add(one[i-1]).add(new BigInteger(Integer.toString((i-1)%2)));
            one[i] = zero[i-1].add(one[i-1]);
        }
        int n;
        while (input.hasNext())
        {
            n = input.nextInt();
            System.out.println(zero[n]);
        }
    }
}
时间: 2024-10-05 19:13:32

ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)的相关文章

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,

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns. Every day after work, Edward will place

ACM学习历程—HDU 5326 Work(树形递推)

Problem Description It’s an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company.As is known to all, every stuff in a company has a title, everyone except the boss has a direct leader, and all

ACM学习历程—SNNUOJ 1110 A Simple Problem(递推 &amp;&amp; 逆元 &amp;&amp; 组合数学 &amp;&amp; 快速幂)(2015陕西省大学生程序设计竞赛K题)

Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N-1 dimension. How many pieces will the “ball” be cut into most?However, it’s impossible to understand the following statement without any explanation.L

ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)

Description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says. "The second problem is, given an positive integer N, we define an equation like this:   N=a[1]+a[2]+a[3]+...+a[m];   a