fork download
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3.  
  4. long unsigned int costArray[30][19];
  5. unsigned int amount;
  6.  
  7. unsigned int currentValue(short int factor2,short int factor3)
  8. { int j;
  9. unsigned int current = amount >> factor2;
  10. for(j=0;j<factor3;j++)
  11. current /= 3;
  12. return current;
  13. }
  14.  
  15. long unsigned int findOptimalAmount(short int factor2,short int factor3)
  16. { unsigned int n = currentValue(factor2,factor3);
  17. if(n < 12)
  18. { costArray[factor2][factor3] = n;
  19. return (long unsigned int)n;
  20. }
  21. else
  22. { if(costArray[factor2][factor3] == 0)
  23. costArray[factor2][factor3] = (findOptimalAmount(factor2+1,factor3) + findOptimalAmount(factor2,factor3+1) + findOptimalAmount(factor2+2,factor3));
  24. return costArray[factor2][factor3];
  25. }
  26. }
  27.  
  28. int main()
  29. { int i,j;
  30. while(scanf("%d",&amount) != EOF)
  31. { for(i=0;i<30;i++)
  32. for(j=0;j<19;j++)
  33. costArray[i][j] = 0;
  34. printf("%lu\n",findOptimalAmount(0,0));
  35. }
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 3300KB
stdin
1000000000
stdout
4243218150