結果

問題 No.1013 〇マス進む
ユーザー sansaquasansaqua
提出日時 2020-03-20 22:22:53
言語 Common Lisp
(sbcl 2.3.8)
結果
WA  
実行時間 -
コード長 6,959 bytes
コンパイル時間 1,561 ms
コンパイル使用メモリ 45,944 KB
実行使用メモリ 47,776 KB
最終ジャッジ日時 2024-05-08 22:39:02
合計ジャッジ時間 8,553 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
26,916 KB
testcase_01 AC 11 ms
30,784 KB
testcase_02 AC 11 ms
26,792 KB
testcase_03 AC 12 ms
26,792 KB
testcase_04 AC 10 ms
26,788 KB
testcase_05 AC 12 ms
26,792 KB
testcase_06 AC 12 ms
28,880 KB
testcase_07 AC 11 ms
26,920 KB
testcase_08 AC 12 ms
30,784 KB
testcase_09 AC 11 ms
27,176 KB
testcase_10 AC 11 ms
27,048 KB
testcase_11 AC 10 ms
26,920 KB
testcase_12 AC 11 ms
27,176 KB
testcase_13 AC 17 ms
29,496 KB
testcase_14 AC 90 ms
37,696 KB
testcase_15 AC 145 ms
39,864 KB
testcase_16 AC 128 ms
37,564 KB
testcase_17 AC 79 ms
33,452 KB
testcase_18 AC 96 ms
35,504 KB
testcase_19 AC 59 ms
31,544 KB
testcase_20 AC 172 ms
41,796 KB
testcase_21 AC 75 ms
33,596 KB
testcase_22 AC 100 ms
37,540 KB
testcase_23 AC 37 ms
31,672 KB
testcase_24 AC 177 ms
41,932 KB
testcase_25 AC 171 ms
45,844 KB
testcase_26 AC 39 ms
33,580 KB
testcase_27 AC 67 ms
35,632 KB
testcase_28 AC 36 ms
29,608 KB
testcase_29 AC 164 ms
41,784 KB
testcase_30 AC 64 ms
33,604 KB
testcase_31 AC 113 ms
37,696 KB
testcase_32 AC 83 ms
37,556 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 AC 173 ms
45,700 KB
testcase_62 AC 167 ms
41,652 KB
testcase_63 AC 10 ms
26,668 KB
testcase_64 AC 11 ms
30,756 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 08 MAY 2024 10:38:52 PM):

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

ソースコード

diff #

(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           ;; enclose the form with VALUES to avoid being captured by LOOP macro
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(defmacro with-buffered-stdout (&body body)
  "Buffers all outputs to *STANDARD-OUTPUT* in BODY and flushes them to
*STANDARD-OUTPUT* after BODY has been done (without error). Note that only
BASE-CHAR is allowed."
  (let ((out (gensym)))
    `(let ((,out (make-string-output-stream :element-type 'base-char)))
       (let ((*standard-output* ,out))
         ,@body)
       (write-string (get-output-stream-string ,out)))))

;;;
;;; Arithmetic operations with static modulus
;;;

;; FIXME: Currently MOD* and MOD+ doesn't apply MOD when the number of
;; parameters is one.
(defmacro define-mod-operations (divisor)
  `(progn
     (defun mod* (&rest args)
       (reduce (lambda (x y) (mod (* x y) ,divisor)) args))

     (eval-when (:compile-toplevel :load-toplevel :execute)
       (sb-c:define-source-transform mod* (&rest args)
         (if (null args)
             1
             (reduce (lambda (x y) `(mod (* ,x ,y) ,',divisor)) args))))

     (defun mod+ (&rest args)
       (reduce (lambda (x y) (mod (+ x y) ,divisor)) args))

     (eval-when (:compile-toplevel :load-toplevel :execute)
       (sb-c:define-source-transform mod+ (&rest args)
         (if (null args)
             0
             (reduce (lambda (x y) `(mod (+ ,x ,y) ,',divisor)) args))))

     (define-modify-macro incfmod (delta)
       (lambda (x y) (mod (+ x y) ,divisor)))

     (define-modify-macro decfmod (delta)
       (lambda (x y) (mod (- x y) ,divisor)))

     (define-modify-macro mulfmod (multiplier)
       (lambda (x y) (mod (* x y) ,divisor)))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setq minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defconstant +max-depth+ 30)
(define-mod-operations +mod+)

(defun main ()
  (let* ((n (read))
         (k (read))
         (ps (make-array n :element-type 'uint31))
         (dp (make-array (list n (+ +max-depth+ 1)) :element-type 'uint31)))
    (dotimes (i n)
      (setf (aref ps i) (- (read-fixnum) 1)))
    (dotimes (i n)
      (setf (aref dp i 0) (+ (aref ps i) 1)))
    (dotimes (depth +max-depth+)
      (dotimes (i n)
        (let* ((delta1 (aref dp i depth))
               (delta2 (aref dp (mod (+ i delta1) n) depth)))
          (setf (aref dp i (+ depth 1))
                (mod+ delta1 delta2)))))
    (with-buffered-stdout
      (dotimes (init n)
        (let ((x init)
              (step 0))
          (loop for depth from +max-depth+ downto 0
                when (<= (+ step (ash 1 depth)) k)
                do (incf step (ash 1 depth))
                   (incf x (aref dp (mod x n) depth)))
          (println (+ x 1)))))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))
0