fork download
  1. printf <- function(...) cat(sprintf(...))
  2.  
  3. solve <- function(v, S)
  4. {
  5. printf("入力\n")
  6. printf("硬貨: [%s]\n", toString(v))
  7. printf("金額: %d\n\n", S)
  8.  
  9. m <- max(v)
  10. N <- rep(Inf, 4 * m)
  11. N[v] <- 1
  12. c <- 0
  13.  
  14. for (i in 1:S) {
  15. if (i > m) {
  16. c <- ifelse(N[i] == N[i - m] + 1, c + 1, 0)
  17. if (c == m) break
  18. }
  19.  
  20. if (i + m > length(N)) N <- c(N, rep(Inf, 4 * m))
  21.  
  22. if (N[i] != Inf) N[i + v] <- pmin(N[i + v], N[i] + 1)
  23. }
  24.  
  25. q <- ifelse(c == m, (S - i) %/% m + 1, 0)
  26. n <- N[S - q * m]
  27. printf("出力\n")
  28. printf("使用枚数: %d\n\n\n", ifelse(n == Inf, -1, n + q))
  29. }
  30.  
  31. V <- list(c(1, 7, 10), c(3, 7, 10), c(10, 19, 23, 47, 101))
  32.  
  33. for (v in V) solve(v, 123456789)
Success #stdin #stdout 0.64s 523260KB
stdin
Standard input is empty
stdout
入力
硬貨: [1, 7, 10]
金額: 123456789

出力
使用枚数: 12345681


入力
硬貨: [3, 7, 10]
金額: 123456789

出力
使用枚数: 12345681


入力
硬貨: [10, 19, 23, 47, 101]
金額: 123456789

出力
使用枚数: 1222348