WOJ1011-Finding Teammates

Big day is coming!
The Wuhan University Collegiate Programming Contest (WHUCPC 2006) will be held in this April. All the ACM lovers, excellent programmers and
computer geniuses in Wuhan University are of great joy, sweeping lots of problems at the Online Judges as preparation, waiting for this big day
to come.However, some students are still in trouble, because they have not found their teammates yet. So the WHUCPC Committee decided to
help these students. All the students that have no teammates are gathered in Peng Kun Square. The method to help them to find their
teammates is quite straightforward: all the students stand in a row, if some consecutive students form an increasing sequence
in their height, then these students must be put in the same team. That is, if there are 4 students who form an increasing sequence,
they must be put in one team and can't be divided into more than one team.
For example, if there are six students, standing as the following figure shows:


Where 1 represents the shortest student, and 6 represents the tallest student, etc.
Since (1, 3, 4, 5) forms an increasing sequence, they should put in the same team, and 2, 6 forms another increasing sequence,
so 2, 6 are put into another team. However, as we all know, the ACM teams are required 3 members each. In the above case, team 1
has 4 members, so the above alignment is invalid. The following figure shows some of the valid alignments:


But in some special cases, if the number of students cannot be divided evenly by 3, we ALLOW the LAST team has less than 3 members.
So if there are 5 students, the follow figure shows some of the valid and invalid alignments:


Now, given the number of the students, your mission is to tell us how many valid alignments there are.
Note, no two students are of the same height.

输入格式

There are several test cases.
Each case contains a single integer n, representing the number of students who need to find teammates. (1<=n<=25)

输出格式

Output an integer in a single line, which is the number of valid alignments. You are ensured that the result is less than 2^63.

样例输入

2
3
4
6
9

样例输出

1
1
3
19
1513

#include
#include
long long f[30];
long long c(long long n,long long k){
	long long a=1,b=1,i;
	if(k>n-k) k=n-k;
	for(i=1;i<=k;i++)
	a=a*(n+1-i);
	for(i=1;i<=k;i++)
	b=b*i;
	return a/b;
} 
void cal(int n){
	int x,i;
	x=n/3;
	for(i=1;i<=x;i++)
	f[n]+=f[3*i-1]*f[n-3*i]*c(n-1,3*i-1);
	if(n%3!=1)
	f[n]+=f[n-1];
}
int main(){
	memset(f,0,sizeof(f));
	f[1]=1;
	f[2]=1;
	f[3]=1;
	int i,n;
	for(i=4;i<=25;i++)
	cal(i); 
	while(scanf("%d",&n)==1)
	printf("%lld\n",f[n]);
	return 0;
} 


你可能感兴趣的:(WOJ)