poj 2680 Computer Transformation (dp+高精度)

来源:互联网 发布:燕十八mysql教程 网盘 编辑:程序博客网 时间:2024/06/16 14:33

Computer Transformation
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4653 Accepted: 1765

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 consequitive zeroes pairs that will appear in the sequence after n steps.

Sample Input

23

Sample Output

11

Source

Southeastern Europe 2005

[Submit]   [Go Back]   [Status]   [Discuss]



题解: dp+高精度

找规律发现f[i]=f[i-1]+f[i-2]*2

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 1003#define LL long longusing namespace std;int f[N][N],n,c[N];void calc(int x,int y,int z){for (int i=0;i<=f[y][0];i++) f[x][i]=f[y][i];memset(c,0,sizeof(c));for (int i=1;i<=f[z][0];i++) c[i]=f[z][i]*2;int t=f[z][0];for (int i=1;i<=t;i++) c[i+1]+=c[i]/10,c[i]%=10;while (c[t+1]) {t++;c[t+1]+=c[t]/10;c[t]%=10;}c[0]=t;for (int i=1;i<=max(t,f[x][0]);i++) f[x][i]+=c[i];    f[x][0]=max(t,f[x][0]);    for (int i=1;i<=f[x][0];i++) f[x][i+1]+=f[x][i]/10,f[x][i]%=10;    while (f[x][f[x][0]+1]){    f[x][0]++;      f[x][f[x][0]+1]+=f[x][f[x][0]]/10;    f[x][f[x][0]]%=10;}}int main(){  f[1][1]=0; f[1][0]=1;  f[2][1]=1; f[2][0]=1;  for (int i=3;i<=1000;i++) {  calc(i,i-1,i-2);  }  while (scanf("%d",&n)!=EOF){   for (int i=f[n][0];i>=1;i--)    printf("%d",f[n][i]);   printf("\n");  }} 


0 0