HDU1134 Game of Connections(大数乘法+打表)

来源:互联网 发布:php怎么在浏览器运行 编辑:程序博客网 时间:2024/06/01 23:37

Game of Connections

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5079    Accepted Submission(s): 2913


Problem Description
This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
 

Input
Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.
 

Output
For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
 

Sample Input
23-1
 

Sample Output
25
 

Source
Asia 2004, Shanghai (Mainland China), Preliminary
 

Recommend
Eddy
 

思路:终于把这个题弄出来了,大数乘法的思想是偷的这位大神的。

#include<cstdio>#include<cstring>#include<iostream>using namespace std;typedef long long ll;const int maxn=1e5+10;int dp[110][201];void add(int *sum, int *part){    int carry=0;    for(int i=1; i<=199; i++)    {        int c2=(part[i]+sum[i]+carry)/10;        sum[i]=(part[i]+sum[i]+carry)%10;        carry=c2;    }}void multi(int *p, int *q, int *k){    int limit1=200, limit2=200;    for(; q[limit1]==0; limit1--);    for(; k[limit2]==0; limit2--);   // cout<<limit1<<" "<<limit2<<endl;    for(int i=1; i<=limit1; i++)    {        int part[201];        memset(part, 0, sizeof(part));        for(int j=1; j<=limit2; j++)        {            part[i+j-1]=q[i]*k[j];        }        add(p, part);    }}void preprg(){    memset(dp, 0, sizeof(dp));    dp[0][1]=1;    for(int i=1; i<=100; i++)    {        for(int j=0; j<i; j++)        {            int res[201];            memset(res, 0, sizeof(res));            multi(res, dp[j], dp[i-1-j]);            add(dp[i], res);        }    }}int main(){    preprg();//打表预处理;    int n;    while(~scanf("%d", &n) && n!=-1)    {        int ed;        for(ed=200; dp[n][ed]==0; ed--);        //cout<<dp[n][3]<<endl;        for(int i=ed; i>=1; i--)            printf("%d", dp[n][i]);        printf("\n");    }    return 0;}



原创粉丝点击