P1464 Function - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
普普通通模拟题意:TLE
#include<iostream>
using namespace std;
int a, b, c;
int w(int a, int b, int c){
if(a<=0||b<=0||c<=0) return 1;
if(a>20||b>20||c>20) return w(20,20,20);
if(a<b&&b<c)
return w(a,b,c-1)+w(a,b-1,c)+w(a,b-1,c-1);
else 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(){
while(true){
cin>>a>>b>>c;
if(a==-1&&b==-1&&c==-1) break;
int t = w(a,b,c);
printf("w(%d, %d, %d) = %d\n",a,b,c,t);
}
return 0;
}
记忆化搜索(递归):记录每次得到的结果
#include<iostream>
using namespace std;
typedef long long LL;//新增数据很大
LL a, b, c;
LL l[22][22][22] = {0};//记忆化搜索
LL w(LL a, LL b, LL c){
if(a<=0||b<=0||c<=0) return 1;
if(a>20||b>20||c>20) return w(20,20,20);
if(l[a][b][c]!=0) return l[a][b][c];
if(a<b&&b<c)
l[a][b][c] = w(a,b,c-1)-w(a,b-1,c)+w(a,b-1,c-1);
else l[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 l[a][b][c];//存储结果
}
int main(){
while(true){
cin>>a>>b>>c;
if(a==-1&&b==-1&&c==-1) break;
LL t = w(a,b,c);
printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,t);
}
return 0;
}