fork download
  1. :- set_prolog_flag(verbose,silent).
  2. :- prompt(_, '').
  3. :- use_module(library(readutil)).
  4. read_num(N):-read_string(user_input, "\n", " ", _, Str),number_string(N,Str).
  5. read_list(Ys):-read_string(user_input, "\n", " ", _, Str),
  6. split_string(Str, " ", "", Xs),
  7. maplist(string_number,Xs,Ys).
  8. string_number(Str,N):-number_string(N,Str).
  9.  
  10. seed(N,M,M2,_):-N<M2,!,false.
  11. seed(N,M,M2,[M3,E]):-MD is M-1,between(1,MD,E),M3 is M2*E.
  12. seed(N,M,M2,Res):-M2a is M2*M,seed(N,M,M2a,Res).
  13.  
  14. seed2(N,E):-member(M,[6,9]),seed(N,M,M,E).
  15.  
  16. calc(Memo,[E0,C0],[N2,C2]):-member([N,C],Memo),
  17. member([D1,D2],[[0,0],[E0,C0]]),
  18. D1=<N,
  19. N2 is N-D1,
  20. C2 is C+D2.
  21.  
  22. dp2(N,C,[],[[N,C]]):-!.
  23. dp2(N,C,[[N,_]|Memo],Res):-!,dp2(N,C,Memo,Res).
  24. dp2(N,C,[[N2,C2]|Memo],[[N,C]|Res]):-dp2(N2,C2,Memo,Res).
  25.  
  26. dp(Memo,[],Memo):-!.
  27. dp(Memo,[E|Seeds],Res):-!,
  28. findall(E2,calc(Memo,E,E2),Memo2),
  29. sort(Memo2,Memo3),
  30. [[N,C]|_]=Memo3,
  31. dp2(N,C,Memo3,Memo4),
  32. sort(Memo4,Memo5),
  33. dp(Memo5,Seeds,Res).
  34.  
  35. calc2([],[]):-!.
  36. calc2([[N,C]|Memo],[R|Res]):-R is N+C,calc2(Memo,Res).
  37.  
  38. main:-
  39. process,
  40.  
  41. process:-
  42. /* your code goes here */
  43. read_num(N),
  44. findall(Es,seed2(N,Es),Seeds),
  45. sort(Seeds,Seeds2),
  46. dp([[N,0]],Seeds2,Res),
  47. calc2(Res,Ans),
  48. sort(Ans,[Ans2|_]),
  49. write(Ans2),
  50.  
  51. :- main.
Success #stdin #stdout #stderr 0.8s 14456KB
stdin
44852
stdout
16
stderr
Warning: /home/NReyO3/prog:10:
	Singleton variables: [M]
Warning: /home/NReyO3/prog:11:
	Singleton variables: [N]