hdoj.1293 The Number of Paths【大数+排列组合】 2015/08/07

来源:互联网 发布:thinkphp3.2项目源码 编辑:程序博客网 时间:2024/06/05 06:27

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
12
 

Sample Output
37
 

Author
SmallBeer (CML)
 

Source
杭电ACM集训队训练赛(VIII)  

注: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;}


0 0