fork(2) download
  1. <?php
  2.  
  3. function constructGraph($bills, $amount){
  4. global $topsOut, $topsIncluded, $weights;
  5. foreach($bills as $nomination => $quantity){
  6. do{
  7. if(($amount-$nomination)>=0 && ($bills[$nomination] - 1)>=0){
  8. array_push($topsOut, $amount);
  9. array_push($topsIncluded, $amount-$nomination);
  10. array_push($weights, $nomination);
  11. $amount = $amount - $nomination;
  12. $bills[$nomination]--;
  13. if($amount==0){
  14. return;
  15. }
  16. }
  17. }while($bills[$nomination]>0 && ($amount-$nomination)>=0);
  18. }
  19. }
  20.  
  21. // $amount = 54500;
  22. // $bills = array(
  23. // 5000 => 200,
  24. // 1000 => 0,
  25. // 500 => 5,
  26. // 100 => 23
  27. // );
  28.  
  29. $amount = 66600;
  30. $bills = array(
  31. 50000 => 2,
  32. 20000 => 3,
  33. 5000 => 1,
  34. 2000 => 4,
  35. 500 => 1,
  36. 200 => 3
  37. );
  38.  
  39. // $amount = 24;
  40. // $bills = array(
  41. // 10 => 1,
  42. // 5 => 5,
  43. // 1 => 5
  44. // );
  45.  
  46. // $amount = 8;
  47. // $bills = array(
  48. // 9 => 1,
  49. // 4 => 1,
  50. // 2 => 5
  51. // );
  52.  
  53. $topsOut = array();
  54. $topsIncluded = array();
  55. $weights = array();
  56. $result = array_fill_keys(array_keys($bills), 0);
  57. $amountSave = $amount;
  58.  
  59. foreach($bills as $nomination => $quantity){
  60. if(($amount-$nomination)>=0 && ($bills[$nomination] - 1)>=0){
  61. array_push($topsOut, $amount);
  62. array_push($topsIncluded, $amount-$nomination);
  63. array_push($weights, $nomination);
  64. $bills[$nomination]--;
  65. constructGraph($bills, $amount-$nomination);
  66. }
  67. }
  68.  
  69. $num = array_search(0, $topsIncluded);
  70. $result[$weights[$num]]++;
  71. if($num){
  72. $n = $topsOut[$num];
  73. while($topsOut[$num]!=$amountSave){
  74. $num--;
  75. $result[$weights[$num]]++;
  76. }
  77. }
  78.  
  79. $resultStr = "";
  80. foreach ($result as $key => $value) {
  81. if($value != 0){
  82. $resultStr = $resultStr."{$value}x{$key} ";
  83. }
  84. }
  85. echo $resultStr."\n";
  86. ?>
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
1x50000