結果

問題 No.7 プライムナンバーゲーム
ユーザー Common Lisp
提出日時 2024-10-23 11:43:48
言語 Common Lisp
(sbcl 2.5.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,224 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 37,808 KB
実行使用メモリ 30,500 KB
最終ジャッジ日時 2024-10-23 11:43:50
合計ジャッジ時間 1,898 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 23 OCT 2024 11:43:48 AM):

; wrote /home/judge/data/code/Main.fasl
; compilation finished in 0:00:00.090

ソースコード

diff #

(defun prime (x)
  (let ((primes (make-array (1+ x) :initial-contents
                  (loop for i from 0 to x
                        collect (if (evenp i) 2 i)))))
    (setf (aref primes 0) 1)
    (loop for i from 3 to (+ 2 (isqrt x)) by 2
          do (when (= (aref primes i) i)
                   (loop for j from (* 2 i) to x by i
                         do (when (= (aref primes j) j)
                                  (setf (aref primes j) i)))))
    (loop for i from 2 to x
          when (= (aref primes i) i)
          collect i)))

(defun main (&rest argv)
  (declare (ignorable argv))
  (let* ((n (read))
         (primes (prime (1+ n)))
         (dp (make-array (1+ n) :initial-element 0)))
    (setf (aref dp 0) 1)
    (setf (aref dp 1) 1)
    (loop for i from 2 to n
          do (block inner
               (loop for p in primes
                     do (when (> p i)
                              (return-from inner))
                        (when (zerop (aref dp (- i p)))
                              (setf (aref dp i) 1)
                              (return-from inner)))))
    (format t "~A~%" (if (zerop (aref dp n))
                         "Lose"
                         "Win"))))

#-swank(main)
0