【高精度+DP】【HDU1223】 OrderCount

Order Count

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


Problem Description
If we connect 3 numbers with "<" and "=", there are 13 cases:
1) A=B=C
2) A=B<C
3) A<B=C
4) A<B<C
5) A<C<B
6) A=C<B
7) B<A=C
8) B<A<C
9) B<C<A
10) B=C<A
11) C<A=B
12) C<A<B
13) C<B<A

If we connect n numbers with "<" and "=", how many cases then?
 

Input
The input starts with a positive integer P(0<P<1000) which indicates the number of test cases. Then on the following P lines, each line consists of a positive integer n(1<=n<=50) which indicates the amount of numbers to be connected.
 

Output
For each input n, you should output the amount of cases in a single line.
 

Sample Input
2
1
3
 

Sample Output
1
13
Hint
Hint
Huge input, scanf is recommended.
 

Author
weigang Lee
 

Source
 



F[i][j]=F[i-1][j-1]*j+F[i-1][j]*j
表示 添加第i个字母的时候 有j个不相等的块(比如 a<b 为两块 a=b 为一块 
显然要写高精度

#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <cstring>  
#include <ctime>  
#include <algorithm>  
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
const int maxn=52; 
const int L = 25;
const int B = 10000;
using namespace std;
struct Bigint
{
    int operator[](int index) const {
        return digit[index];
    }

    int& operator[](int index) {
        return digit[index];
    }
	int len;
	int digit[L];
};
Bigint F[52][52];
void MEMSET(Bigint &a)
{
	a.len=1;
	memset(a.digit,0,sizeof(a.digit));
}
void add(Bigint &d,const Bigint &a,const Bigint &b)
{
	int temp;
	Bigint c;
	MEMSET(c);
	c.len=max(a.len,b.len)+1;
	for(int i=0,temp=0;i<c.len;i++)
	{
		temp+=a[i]+b[i];
		c[i]=temp%B;
		temp=temp/B;
	}
	if(c[c.len-1]==0) c.len--;
	d=c;
}
void highXlow(Bigint &d,Bigint &a,int &b)
{
	int temp;
	Bigint c;
	MEMSET(c);
	c.len=a.len+1;
	for(int i=0,temp=0;i<c.len;i++)
	{
		temp+=a[i]*b;
		c[i]=temp%B;
		temp=temp/B;
	}
	if(c[c.len-1]==0) c.len--;
	d=c;
}
Bigint ANS[52];
void YCL()
{
	F[1][1].digit[0]=1;
	F[1][1].len=1;
	ANS[1].digit[0]=1;
	ANS[1].len=1;
	for(int i=2;i<=50;i++)
	 for(int j=1;j<=i;j++)
	 {
	 	add(F[i][j],F[i-1][j],F[i-1][j-1]);
	 	highXlow(F[i][j],F[i][j],j);
	 	add(ANS[i],ANS[i],F[i][j]);
	 }
}
void outputBigint(Bigint &ans)
{
	if(ans.len>=1) printf("%d",ans[ans.len-1]);
	for(int i=ans.len-2;i>=0;i--)
	printf("%04d",ans[i]);
	printf("\n");
}
int main()
{
	int T,n;
	cin>>T;
	YCL();
	while(T--)
	{
		cin>>n;
		outputBigint(ANS[n]);	
	}
	return 0;
}
  



posted on 2014-10-28 18:53  DDUPzy  阅读(496)  评论(0编辑  收藏  举报

导航