vijos1080 函数的函数
题目
原题链接
对于一个递归函数w(a,b,c)
如果a<=0 or b<=0 or c<=0就返回值1.
如果a>20 or b>20 or c>20就返回w(20,20,20)
如果a
算法
- 记忆化搜索
思路
题目最后一段:当a,b,c均为15时,调用的次数将非常的多。你要想个办法才行.
我一看,又是数学?!?!推推推推推,但是看到20,20,20的max时我想——记忆化搜索跑20^3次没问题啊?!
然后想了一下,打了出来,AC
代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long remeber[21][21][21];
long long w(int a,int b,int c)
{
//严格按照题目顺序模拟
if(a<=0 or b<=0 or c<=0)
{
return 1;
}
if(a>20 or b>20 or c>20)
{
a=b=c=20;
}
if(remeber[a][b][c]!=0) return remeber[a][b][c];//记忆化往后放,不然会RE
if(a<b and b<c)
{
remeber[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
}
else
{
remeber[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 remeber[a][b][c];
}
int main()
{
int a,b,c;
cin>>a>>b>>c;
while(not(a==-1 and b==-1 and c==-1))
{
cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;
cin>>a>>b>>c;
}
}