HDU-1041-Computer Transformation,大数递推,水过~~

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Computer Transformation

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
Time Limit: 2000/1000 MS (Java/Others) ??

?Memory Limit: 65536/32768 K (Java/Others)

http://acm.hdu.edu.cn/showproblem.php?pid=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. 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

Southeastern Europe 2005

? ? 似曾相识的一题,连递推数列都是一模一样的,好醉。。

? ? 其实举几组样例多举几组就可以发现规律了,每次出现1001这种时都是上一个序列中有01这种子串,所以看上一串有多少个01字串就知道下一个有多少个1001,所以不难发现递推公式a[i]=a[i-1]+2*a[i-2](i>=3);这个和那个填骨牌的题一模一样,但注意数据范围,就算2的1000次方也是很大的,所以用二维数组存储大数,开到400就够了;

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
//const int INF=0x3f3f3f3f;
const int N=1000+10;
int a[N][400];
int main()
{
    memset(a,sizeof(a));
    int n,i,j;
    a[1][0]=0,a[2][0]=1;
    int c=0;
    for(i=3;i<=1000;i++)
    {
        c=0;
        for(j=0;j<=400;j++)//核心--大数;
        {
            a[i][j]=a[i-1][j]+2*a[i-2][j]+c;
            c=a[i][j]/10;
            a[i][j]%=10;
        }
    }
    while(~scanf("%d",&n))
    {
        if(n==1)
            printf("0\n");
        else
        {
            for(j=399;j>=0;j--)
                if(a[n][j])
                break;
            for(i=j;i>=0;i--)
                printf("%d",a[n][i]);
            printf("\n");
        }
    }
    return 0;
}

? 其实静下心来想想思路到A出这道题不过10多分钟,这如果在比赛中就会有绝对的优势。

相关文章

发表回复

您的电子邮箱地址不会被公开。