http://acm.fjnu.edu.cn/show?problem_id=1872
如果直接用递归函数肯定会tle,有两种解决方案:
1)先用暴力法列出数值表,找规律,或用数学方法推导,直接找到递推式。
2)在程序开始先算出所有题目要求的数值范围,并保存,在主程序里直接调用就行了。
#include <stdio.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int a,b,c,w[21][21][21];
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(a=0;a<21;a++)
{
for(b=0;b<21;b++)
w[0][a][b]=w[a][0][b]=w[a][b][0]=1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(a=1;a<21;a++)
{
for(b=1;b<21;b++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(c=1;c<21;c++)
{
if(a<b && b<c)
w[a][b][c]=w[a][b][c-1] + w[a][b-1][c-1] - w[a][b-1][c];
else
w[a][b][c]=w[a-1][b][c] + w[a-1][b-1][c] + w[a-1][b][c-1] - w[a-1][b-1][c-1];
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(scanf("%d %d %d",&a,&b,&c),a!=-1||b!=-1||c!=-1)
{
if(a <= 0 || b <= 0 || c <= 0)
printf("w(%d, %d, %d) = %d\n",a,b,c,1);
else if(a > 20 || b > 20 || c > 20)
printf("w(%d, %d, %d) = %d\n",a,b,c,w[20][20][20]);
else
printf("w(%d, %d, %d) = %d\n",a,b,c,w[a][b][c]);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
return 0;
}