The Number of Paths
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 595 Accepted Submission(s): 273
Problem Description
Let f (n) be the number of paths with n steps starting from O (0, 0), with steps of the type (1, 0), or (-1, 0), or (0, 1), and never intersecting themselves. For instance, f (2) =7, as shown in Fig.1. Equivalently, letting E=(1,0),W=(-1,0),N=(0,1), we want the number of words A1A2...An, each Ai either E, W, or N, such that EW and WE never appear as factors.

Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of steps(1<=n<=1000).
Output
For each test case, there is only one integer means the number of paths.
Sample Input
1 2
Sample Output
3 7
Author
SmallBeer (CML)
Source
注:f(n) = 2*f(n-1)+f(n-2)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int p[1010][500];
int main(){
//int p[1010][500];
memset(p,0,sizeof(p));
p[0][1] = 1;
p[1][1] = 3;
p[0][0] = p[1][0] = 1;
for( int i = 2 ; i <= 1000 ; ++i ){
for( int j = 1 ; j <= p[i-1][0] ; ++j )
p[i][j] = p[i-1][j] * 2 + p[i-2][j];
for( int j = 1 ; j <= p[i-1][0] ; ++j ){
if( p[i][j] > 9 ){
p[i][j+1] += p[i][j] / 10;
p[i][j] %= 10;
}
}
p[i][0] = p[i][p[i-1][0]+1] ? p[i-1][0] + 1 : p[i-1][0] ;
}
int n;
while(cin>>n){
for( int i = p[n][0] ; i > 0 ; --i )
printf("%d",p[n][i]);
printf("\n");
}
return 0;
}