fork(1) 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 = 6600;
  30. $bills = array(
  31. 5000 => 1,
  32. 2000 => 4,
  33. 500 => 1,
  34. 200 => 3
  35. );
  36.  
  37. // $amount = 24;
  38. // $bills = array(
  39. // 10 => 1,
  40. // 5 => 5,
  41. // 1 => 5
  42. // );
  43.  
  44. // $amount = 8;
  45. // $bills = array(
  46. // 9 => 1,
  47. // 4 => 1,
  48. // 2 => 5
  49. // );
  50.  
  51. $topsOut = array();
  52. $topsIncluded = array();
  53. $weights = array();
  54. $result = array_fill_keys(array_keys($bills), 0);
  55. $amountSave = $amount;
  56.  
  57. foreach($bills as $nomination => $quantity){
  58. if(($amount-$nomination)>=0 && ($bills[$nomination] - 1)>=0){
  59. array_push($topsOut, $amount);
  60. array_push($topsIncluded, $amount-$nomination);
  61. array_push($weights, $nomination);
  62. $bills[$nomination]--;
  63. constructGraph($bills, $amount-$nomination);
  64. }
  65. }
  66.  
  67. $num = array_search(0, $topsIncluded);
  68. $result[$weights[$num]]++;
  69. if($num){
  70. $n = $topsOut[$num];
  71. while($topsOut[$num]!=$amountSave){
  72. $num--;
  73. $result[$weights[$num]]++;
  74. }
  75. }
  76.  
  77. $resultStr = "";
  78. foreach ($result as $key => $value) {
  79. if($value != 0){
  80. $resultStr = $resultStr."{$value}x{$key} ";
  81. }
  82. }
  83. echo $resultStr."\n";
  84. ?>
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
3x2000 3x200