spoj.com/problems/COINS/
I got WA in this problem.
But in ideone with some test cases, it gives correct answer.. Can anyone help me out?
this is my source code : ideone.com/mOroGm
#include <stdio.h>
#include <map>
using namespace std;
map<long long int, long long int> mymap;
long long int exchangeCoin(long long int byteland);
int main() {
long long int inputCoin;
bool button = false;
while(scanf("%lld", &inputCoin) != EOF){
if(button == false) button = true;
else printf("\n");
if(inputCoin != 0){
long long int result = exchangeCoin(inputCoin);
printf("%lld", result);
}
else{
printf("%lld", 0);
}
}
return 0;
}
long long int exchangeCoin(long long int byteland){
map<long long int, long long int>::iterator it;
it = mymap.find(byteland);
if(it != mymap.end()){
return it->second;
}
else{
long long int a = byteland/2;
long long int b = byteland/3;
long long int c = byteland/4;
if(a+b+c < byteland){
return byteland;
}
else{
long long int resultA = exchangeCoin(a);
long long int resultB = exchangeCoin(b);
long long int resultC = exchangeCoin(c);
mymap.insert(pair<long long int, long long int>(byteland, resultA+resultB+resultC));
return (resultA + resultB + resultC);
}
}
}
Many thanks.