fork download
  1. // http://w...content-available-to-author-only...j.com/problems/COINS/
  2. // segmentation error(SIGSEGV), map maximum size over problem
  3. #include <stdio.h>
  4. #include <map>
  5. using namespace std;
  6.  
  7. map<long long int, long long int> mymap;
  8.  
  9. long long int exchangeCoin(long long int byteland);
  10.  
  11. int main() {
  12. long long int inputCoin;
  13. bool button = false;
  14. while(scanf("%lld", &inputCoin) != EOF){
  15. if(button == false) button = true;
  16. else printf("\n");
  17. if(inputCoin != 0){
  18. long long int result = exchangeCoin(inputCoin);
  19. printf("%lld", result);
  20. }
  21. else{
  22. printf("%lld", 0);
  23. }
  24. }
  25. return 0;
  26. }
  27.  
  28. long long int exchangeCoin(long long int byteland){
  29. map<long long int, long long int>::iterator it;
  30. it = mymap.find(byteland);
  31. if(it != mymap.end()){
  32. return it->second;
  33. }
  34. else{
  35. long long int a = byteland/2;
  36. long long int b = byteland/3;
  37. long long int c = byteland/4;
  38.  
  39. if(a+b+c < byteland){
  40. return byteland;
  41. }
  42. else{
  43. long long int resultA = exchangeCoin(a);
  44. long long int resultB = exchangeCoin(b);
  45. long long int resultC = exchangeCoin(c);
  46. mymap.insert(pair<long long int, long long int>(byteland, resultA+resultB+resultC));
  47.  
  48. return (resultA + resultB + resultC);
  49. }
  50. }
  51. }
Success #stdin #stdout 0s 3232KB
stdin
12
2
stdout
13
2