给你一个参考吧,这里只优化了abc三个数相等的情况
#include<cstdio>
long long int w(long long int,long long int,long long int);
struct result
{
long long int x;
long long int y;
long long int z;
int sum;
}e[8500];
int t=0;
int main()
{
long long int a,b,c;
int res;
while((scanf("%lld%lld%lld",&a,&b,&c)==3)&&(!((a==-1)&&(b==-1)&&(c==-1))))
{
res=w(a,b,c);
printf("w(%lld, %lld, %lld) = %d\n",a,b,c,res);
}
return 0;
}
long long int w(long long int a,long long int b,long long int c)
{
for(int i=0;i<t;i++)
{
if((e[i].x==a)&&(e[i].y==b)&&(e[i].z==c))
{
return e[i].sum;
}
}
if((a<=0)||(b<=0)||(c<=0))
{
return 1;
}
if((a>=20)||(b>=20)||(c>=20))
{
return 1048576;
}
if((a==b)&&(b==c))
{
int res=1;
for(int i=1;i<=a;i++)
{
res*=2;
}
return res;
}
if((a<b)&&(b<c))
{
//return w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
int res=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
e[t].x=a;e[t].y=b;e[t].z=c;
e[t].sum=res;
t++;
return res;
}
//return w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
int res=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
e[t].x=a;e[t].y=b;e[t].z=c;
e[t].sum=res;
t++;
return res;
}
by Swanwan_OWO @ 2018-08-30 23:07:39
```c
#include<cstdio>
long long int w(long long int,long long int,long long int);
struct result
{
long long int x;
long long int y;
long long int z;
int sum;
}e[8500];
int t=0;
int main()
{
long long int a,b,c;
int res;
while((scanf("%lld%lld%lld",&a,&b,&c)==3)&&(!((a==-1)&&(b==-1)&&(c==-1))))
{
res=w(a,b,c);
printf("w(%lld, %lld, %lld) = %d\n",a,b,c,res);
}
return 0;
}
long long int w(long long int a,long long int b,long long int c)
{
for(int i=0;i<t;i++)
{
if((e[i].x==a)&&(e[i].y==b)&&(e[i].z==c))
{
return e[i].sum;
}
}
if((a<=0)||(b<=0)||(c<=0))
{
return 1;
}
if((a>=20)||(b>=20)||(c>=20))
{
return 1048576;
}
if((a==b)&&(b==c))
{
int res=1;
for(int i=1;i<=a;i++)
{
res*=2;
}
return res;
}
if((a<b)&&(b<c))
{
//return w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
int res=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
e[t].x=a;e[t].y=b;e[t].z=c;
e[t].sum=res;
t++;
return res;
}
//return w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
int res=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
e[t].x=a;e[t].y=b;e[t].z=c;
e[t].sum=res;
t++;
return res;
}
```
by Swanwan_OWO @ 2018-08-30 23:08:44
```cpp
#include <bits/stdc++.h>
using namespace std;
long f[21][21][21];
long w(long a,long b,long c){
if(a<=0||b<=0||c<=0){
return 1;
}
if(a>20||b>20||c>20){
return w(20,20,20);
}
if(f[a][b][c]!=0){
return f[a][b][c];
}
if(a<b&&b<c){
f[a][b][c] = w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
return w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
}
f[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);
return w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
int main(){
long x,y,z;
while(cin>>x>>y>>z){
if(x==-1&&y==-1&&z==-1){
break;
}
cout<<"w("<<x<<", "<<y<<", "<<z<<") = "<<w(x,y,z)<<endl;
}
return 0;
}
```
by Free_S @ 2018-09-01 14:23:00
记忆化搜索
by Free_S @ 2018-09-01 14:23:26