(defconstant +mod+ 1000000009) (defconstant +max+ 1000000) (defun %mod-inverse (x) (if (= x 1) 1 (mod (* (- +mod+ (floor +mod+ x)) (%mod-inverse (mod +mod+ x))) +mod+))) (defun main (&rest argv) (declare (ignorable argv)) (let* ((query (read)) (dp (make-array +max+ :initial-element 1 :element-type 'integer)) (coins (list 5 10 50 100 500))) (dolist (coin coins) (loop for i from coin below +max+ do (setf (aref dp i) (mod (+ (aref dp i) (aref dp (- i coin))) +mod+)))) (dotimes (_ query) (let* ((p (read)) (q (floor p 500)) (r (mod p 500)) (res 0)) (dotimes (i 6) (let ((s 1)) (dotimes (j 6) (when (/= i j) (setq s (mod (* s (- (+ q +mod+) j)) +mod+)))) (dotimes (j 6) (when (/= i j) (setq s (mod (* s (%mod-inverse (mod (- (+ i +mod+) j) +mod+))) +mod+)))) (setq res (mod (+ res (* s (aref dp (+ (* 500 i) r)))) +mod+)))) (format t "~d~%" res))))) (main)