Let's write β

プログラミング中にできたことか、思ったこととか

JAG2009 ProblemA

Luck Manipulator

(defun next-rand (a b c x)
  (mod (+ (* a x) b) c))

(defun manip-slot (y-list a b c x)
  (labels
      ((%manup-slot (y-list a b c x frm)
	 (cond ((null y-list)
		;;前のフレームでゲームが終了していたという事なので
		(1- frm))
	       ;;達成不能
	       ((= (1- frm) 10000) -1)
	       (t
		(if (= x (car y-list))
		    (%manup-slot (cdr y-list) a b c
				 (next-rand a b c x)
				 (1+ frm))
		    (%manup-slot y-list a b c
				 (next-rand a b c x)
				 (1+ frm)))))))
    (%manup-slot y-list a b c x 0)))

(defun main ()
  (loop
     for n = (read)
     for a = (read)
     for b = (read)
     for c = (read)
     for x = (read)
     until (every #'zerop `(,n ,a ,b ,c ,x))
     do
       (format t "~A~%"
	       (manip-slot
		(loop for i from 1 upto n
		     collect (read))
		a b c x))))

単純ですね。再帰で簡単に。