I'm trying to code a function which has an unsigned int as output, and two unsigned ints as inputs. Now since this function has been defined recursively, i was trying to implement memoization using std::map to increase the time efficiency.
code:
unsigned memo_ack(unsigned m,unsigned n)
{
static map <pair<int,int>,unsigned> mem;
unsigned ans;
if(m==0)
return n+1;
else if(n==0)
if(mem.count(ackpair<m,n>)>0)
}
Here, I wish to use the count function to see whether a particular value with inputs (m,n) is present or not.
For example, if the count function returns > 0, then, the function will just return the stored value of the function, which has previously been calculated, so that there is no need of recalculation. If the count function returns zero, the original function calculates the value corresponding to that pair using the function definition.
Here's the problem: The count function won't accept std::pair as argument, nor will it accept two input arguments. How else do I tell the count function to count the number of occurrences of a particular input pair, and if it already exists, return a positive number(1, in this case)?
On passing a std::pair, I get the following error: Invalid operands to binary expression ('pair' and 'unsigned int')
The original non-memoized recursive solution was :
#include<iostream>
using namespace std;
int myAckermann(int m, int n)
{
if(m==0)
return n+1;
else if (n==0)
return myAckermann(m-1,1);
else
return myAckermann(m-1,myAckermann(m,n-1));
}
int main()
{
int m,n;
cin>>m>>n;
long long ans;
ans = myAckermann(m,n);
cout<<ans;
return 0;
}