結果

問題 No.1140 EXPotentiaLLL!
ユーザー Common LispCommon Lisp
提出日時 2024-11-03 02:29:39
言語 Common Lisp
(sbcl 2.3.8)
結果
AC  
実行時間 1,981 ms / 2,000 ms
コード長 2,157 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 43,912 KB
実行使用メモリ 71,188 KB
最終ジャッジ日時 2024-11-03 02:29:58
合計ジャッジ時間 18,145 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,981 ms
69,032 KB
testcase_01 AC 1,934 ms
65,192 KB
testcase_02 AC 1,927 ms
71,184 KB
testcase_03 AC 1,409 ms
67,104 KB
testcase_04 AC 1,078 ms
71,188 KB
testcase_05 AC 1,671 ms
65,196 KB
testcase_06 AC 1,556 ms
67,152 KB
testcase_07 AC 1,896 ms
65,320 KB
testcase_08 AC 82 ms
69,052 KB
testcase_09 AC 81 ms
67,276 KB
testcase_10 AC 81 ms
69,180 KB
testcase_11 AC 82 ms
69,180 KB
testcase_12 AC 80 ms
65,196 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 03 NOV 2024 02:29:40 AM):

; file: /home/judge/data/code/Main.lisp
; in: DEFUN SIEVE-OF-ATKIN
;     (/ SLIM (SQRT 3))
; 
; note: unable to
;   convert to multiplication by reciprocal
; because:
;   1.7320508 does not have an exact reciprocal

;     (/ SLIM (SQRT 2))
; 
; note: unable to
;   convert to multiplication by reciprocal
; because:
;   1.4142135 does not have an exact reciprocal
; 
; compilation unit finished
;   printed 2 notes


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

ソースコード

diff #

(declaim (optimize (speed 3) (safety 0)))
(defun sieve-of-atkin (lim)
  (declare (type fixnum lim))
  (let* ((slim (sqrt lim))
         (p (make-array (+ lim 60) :element-type 'fixnum :initial-element 0)))
    
    (declare (type short-float slim))
    (loop for v from 1 to (floor slim 2) do
          (let ((x (1+ (* 4 v v)))
                (y 8))
            (declare (type fixnum x))
            (declare (type fixnum y))
            (loop while (<= x lim) do
                  (when (not (= (mod x 12) 9))
                        (setf (aref p x) (logxor (aref p x) 1)))
                  (setf x (+ x y)
                        y (+ y 8)))))
    (loop for v from 1 to (floor (/ slim (sqrt 3))) by 2 do
          (let ((x (+ (* 3 v v) 4))
                (y 12))
            (declare (type fixnum x))
            (declare (type fixnum y))
            (loop while (<= x lim) do
                  (when (= (mod x 12) 7)
                        (setf (aref p x) (logxor (aref p x) 1)))
                  (setf x (+ x y)
                        y (+ y 8)))))
    (loop for v from 2 to (floor (/ slim (sqrt 2))) do
          (let ((x (1- (* 2 v (1+ v))))
                (y (- (* 4 v) 8)))
            (loop while (and (<= x lim) (<= 0 y)) do
                  (when (= (mod x 12) 11)
                        (setf (aref p x) (logxor (aref p x) 1)))
                  (setf x (+ x y)
                        y (- y 8)))))
    (loop for n from 5 to (floor slim) do
          (when (/= (aref p n) 0)
                (loop for z from (* n n) below lim by (* n n) do
                      (setf (aref p z) 0))))
    (setf (aref p 2) 1
          (aref p 3) 1)
    p))

(defun main (&rest argv)
  (declare (ignorable argv))
  (let* ((q (read))
         (p (sieve-of-atkin 5000001)))
    (declare (type fixnum q))
    (dotimes (_ q)
      (let* ((a (read))
             (s (read)))
        (declare (type fixnum a))
        (declare (type fixnum s))
        (format t "~d~%" (if (= (aref p s) 1)
                             (if (zerop (mod a s))
                                 0
                                 1)
                             -1))))))

(main)
0