結果

問題 No.1443 Andd
ユーザー sansaquasansaqua
提出日時 2021-03-26 22:47:35
言語 Common Lisp
(sbcl 2.3.8)
結果
TLE  
実行時間 -
コード長 15,927 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 54,096 KB
実行使用メモリ 37,660 KB
最終ジャッジ日時 2023-08-19 09:26:46
合計ジャッジ時間 10,917 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
27,792 KB
testcase_01 AC 9 ms
27,836 KB
testcase_02 AC 10 ms
27,720 KB
testcase_03 AC 27 ms
27,940 KB
testcase_04 AC 48 ms
27,992 KB
testcase_05 AC 30 ms
27,900 KB
testcase_06 AC 26 ms
27,932 KB
testcase_07 AC 45 ms
28,048 KB
testcase_08 AC 39 ms
31,700 KB
testcase_09 AC 11 ms
27,924 KB
testcase_10 AC 60 ms
27,996 KB
testcase_11 AC 50 ms
28,040 KB
testcase_12 AC 49 ms
30,096 KB
testcase_13 AC 1,077 ms
28,968 KB
testcase_14 AC 1,077 ms
29,052 KB
testcase_15 AC 1,077 ms
28,972 KB
testcase_16 AC 1,080 ms
32,728 KB
testcase_17 AC 1,075 ms
28,964 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 19 AUG 2023 09:26:35 AM):
; processing (IN-PACKAGE :CL-USER)
; processing (DEFPARAMETER *OPT* ...)
; processing (SET-DISPATCH-MACRO-CHARACTER #\# ...)
; processing (DOLIST (F #) ...)
; processing (SETQ *RANDOM-STATE* ...)
; processing (DEFINE-INT-TYPES 2 ...)
; processing (DEFCONSTANT +MOD+ ...)
; processing (DEFMACRO DBG ...)
; processing (DECLAIM (INLINE PRINTLN))
; processing (DEFUN PRINTLN ...)
; processing (DEFPACKAGE :CP/READ-FIXNUM ...)
; processing (IN-PACKAGE :CP/READ-FIXNUM)
; processing (DECLAIM (FTYPE # ...))
; processing (DEFUN READ-FIXNUM ...)
; processing (DEFPACKAGE :CP/BIT-BASHER ...)
; processing (IN-PACKAGE :CP/BIT-BASHER)
; processing (ASSERT (= SB-VM:N-WORD-BITS ...))
; processing (DEFMACRO U64-DPB ...)
; processing (DEFCONSTANT +MOST-POSITIVE-WORD+ ...)
; processing (DEFUN BIT-NOT! ...)
; processing (DECLAIM (FTYPE # ...))
; processing (DEFUN BIT-FILL! ...)
; file: /home/judge/data/code/Main.lisp
; in: DEFUN BIT-FILL!
;     (CP/BIT-BASHER::U64-DPB
;      (LDB (BYTE (- CP/BIT-BASHER::END%64 CP/BIT-BASHER::START%64) 0)
;           CP/BIT-BASHER::MASK)
;      (BYTE (- CP/BIT-BASHER::END%64 CP/BIT-BASHER::START%64)
;            CP/BIT-BASHER::START%64)
;      (SB-KERNEL:%VECTOR-RAW-BITS CP/BIT-BASHER::SB-VECTOR
;                                  CP/BIT-BASHER::START/64))
; --> LET* LOGIOR THE 
; ==>
;   (ASH
;    (LOGAND
;     (LDB (BYTE (- CP/BIT-BASHER::END%64 CP/BIT-BASHER::START%64) 0)
;          CP/BIT-BASHER::MASK)
;     #:G13)
;    #:G12)
; 
; note: forced to do full call
;       unable to do inline ASH (cost 3) because:
;       The first argument is a (UNSIGNED-BYTE 63), not a FIXNUM.
;       The result is a (VALUES (MOD 85070591730234615856620279821087277057)
;                               &OPTIONAL), not a (VALUES FIXNUM &REST T).
;       unable to do inline ASH (cost 3) because:
;       The first argument is a (UNSIGNED-BYTE 63), not a FIXNUM.
;       The result is a (VALUES (M

ソースコード

diff #

(in-package :cl-user)
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter *opt*
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (ql:quickload '(:cl-debug-print :fiveam :cp/util) :silent t)
  #+swank (use-package :cp/util :cl-user)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t))))
  #+sbcl (dolist (f '(:popcnt :sse4)) (pushnew f sb-c:*backend-subfeatures*))
  #+sbcl (setq *random-state* (seed-random-state (nth-value 1 (get-time-of-day)))))
#-swank (eval-when (:compile-toplevel)
          (setq *break-on-signals* '(and warning (not style-warning))))
#+swank (set-dispatch-macro-character #\# #\> #'cl-debug-print:debug-print-reader)

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

(defconstant +mod+ 1000000007)

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

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

;; BEGIN_INSERTED_CONTENTS
(defpackage :cp/read-fixnum
  (:use :cl)
  (:export #:read-fixnum))
(in-package :cp/read-fixnum)

(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))))))))

(defpackage :cp/bit-basher
  (:use :cl)
  (:export #:bit-not! #:bit-fill! #:bit-count #:bit-lshift #:bit-rshift #:bit-shift)
  (:documentation "Provides several operations on bit vector that are not
included in the standard."))
(in-package :cp/bit-basher)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (assert (= sb-vm:n-word-bits 64)))

;; KLUDGE: a variant of DPB that handles a 64-bit word efficiently
(defmacro u64-dpb (new spec int)
  (destructuring-bind (byte s p) spec
    (assert (eql 'byte byte))
    (let ((size (gensym)) (posn (gensym)) (mask (gensym)))
      `(let* ((,size ,s)
              (,posn ,p)
              (,mask (ldb (byte ,size 0) -1)))
         (logior (the (unsigned-byte 64) (ash (logand ,new ,mask) ,posn))
                 (the (unsigned-byte 64) (logand ,int (lognot (ash ,mask ,posn)))))))))

(defconstant +most-positive-word+ (ldb (byte 64 0) -1))

(defun bit-not! (sb-vector &optional (start 0) end)
  "Destructively flips the bits in the range [START, END)."
  (declare (optimize (speed 3))
           (simple-bit-vector sb-vector)
           ((mod #.array-total-size-limit) start)
           ((or null (mod #.array-total-size-limit)) end))
  (setq end (or end (length sb-vector)))
  (assert (<= start end (length sb-vector)))
  (multiple-value-bind (start/64 start%64) (floor start 64)
    (multiple-value-bind (end/64 end%64) (floor end 64)
      (declare (optimize (safety 0)))
      (if (= start/64 end/64)
          (setf (sb-kernel:%vector-raw-bits sb-vector start/64)
                (u64-dpb (ldb (byte (- end%64 start%64) start%64)
                              (logxor +most-positive-word+ (sb-kernel:%vector-raw-bits sb-vector start/64)))
                         (byte (- end%64 start%64) start%64)
                         (sb-kernel:%vector-raw-bits sb-vector start/64)))
          (progn
            (setf (sb-kernel:%vector-raw-bits sb-vector start/64)
                  (dpb (sb-kernel:%vector-raw-bits sb-vector start/64)
                       (byte start%64 0)
                       (logxor +most-positive-word+ (sb-kernel:%vector-raw-bits sb-vector start/64))))
            (loop for i from (+ 1 start/64) below end/64
                  do (setf (sb-kernel:%vector-raw-bits sb-vector i)
                           (logxor +most-positive-word+ (sb-kernel:%vector-raw-bits sb-vector i))))
            (unless (zerop end%64)
              (setf (sb-kernel:%vector-raw-bits sb-vector end/64)
                    (dpb (logxor +most-positive-word+ (sb-kernel:%vector-raw-bits sb-vector end/64))
                         (byte end%64 0)
                         (sb-kernel:%vector-raw-bits sb-vector end/64))))))))
  sb-vector)

(declaim (ftype (function * (values simple-bit-vector &optional)) bit-fill!))
(defun bit-fill! (sb-vector bit &optional (start 0) end)
  "Destructively sets the bits in the range [START, END) to BIT."
  (declare (optimize (speed 3))
           (simple-bit-vector sb-vector)
           (bit bit)
           ((mod #.array-total-size-limit) start)
           ((or null (mod #.array-total-size-limit)) end))
  (setq end (or end (length sb-vector)))
  (assert (<= start end (length sb-vector)))
  (let ((mask (if (zerop bit) 0 +most-positive-word+)))
    (multiple-value-bind (start/64 start%64) (floor start 64)
      (multiple-value-bind (end/64 end%64) (floor end 64)
        (if (= start/64 end/64)
            (setf (sb-kernel:%vector-raw-bits sb-vector start/64)
                  (u64-dpb (ldb (byte (- end%64 start%64) 0) mask)
                           (byte (- end%64 start%64) start%64)
                           (sb-kernel:%vector-raw-bits sb-vector start/64)))
            (progn
              (setf (sb-kernel:%vector-raw-bits sb-vector start/64)
                    (u64-dpb (sb-kernel:%vector-raw-bits sb-vector start/64)
                             (byte start%64 0)
                             mask))
              (loop for i from (+ 1 start/64) below end/64
                    do (setf (sb-kernel:%vector-raw-bits sb-vector i) mask))
              (unless (zerop end%64)
                (setf (sb-kernel:%vector-raw-bits sb-vector end/64)
                      (dpb mask
                           (byte end%64 0)
                           (sb-kernel:%vector-raw-bits sb-vector end/64)))))))))
  sb-vector)

;; (count 1 simple-bit-vector) is sufficiently fast on SBCL when handling whole
;; vector. If START or END are specified, however, it is slow as the
;; deftransform for COUNT doesn't work. See
;; https://github.com/sbcl/sbcl/blob/cd7af0d5b15e98e21ace8ef164e0f39019e5ed4b/src/compiler/generic/vm-tran.lisp#L484-L527
(defun bit-count (sb-vector &optional (start 0) end)
  "Counts 1's in the range [START, END)."
  (declare (optimize (speed 3))
           (simple-bit-vector sb-vector)
           ((mod #.array-total-size-limit) start)
           ((or null (mod #.array-total-size-limit)) end))
  (setq end (or end (length sb-vector)))
  (assert (<= start end (length sb-vector)))
  (multiple-value-bind (start/64 start%64) (floor start 64)
    (multiple-value-bind (end/64 end%64) (floor end 64)
      (declare (optimize (safety 0)))
      (if (= start/64 end/64)
          (logcount (ldb (byte (- end%64 start%64) start%64)
                         (sb-kernel:%vector-raw-bits sb-vector start/64)))
          (let ((result 0))
            (declare ((mod #.array-total-size-limit) result))
            (incf result (logcount (ldb (byte (- 64 start%64) start%64)
                                        (sb-kernel:%vector-raw-bits sb-vector start/64))))
            (loop for i from (+ 1 start/64) below end/64
                  do (incf result (logcount (sb-kernel:%vector-raw-bits sb-vector i))))
            (unless (zerop end%64)
              (incf result (logcount (ldb (byte end%64 0)
                                          (sb-kernel:%vector-raw-bits sb-vector end/64)))))
            result)))))

(declaim (ftype (function * (values simple-bit-vector &optional)) bit-lshift))
(defun bit-lshift (bit-vector delta &optional result-vector end)
  "Left-shifts BIT-VECTOR by DELTA bits and fills the new bits with zero.
The behaviour is the same as the bit-wise operations in ANSI CL: The result is
copied to RESULT-VECTOR; if it is T, BIT-VECTOR is destructively modified; if it
is NIL, a new bit-vector of the same length is created. If END is specified,
this function shifts only the range [0, END) of BIT-VECTOR and copies it to the
range [0, END+DELTA) of RESULT-VECTOR.

Note that here `left' means the direction from a smaller index to a larger one
and is contrary to the `visual' direction: i.e. (bit-lshift #*1011000 2) |->
#*0010110."
  (declare (optimize (speed 3))
           (simple-bit-vector bit-vector)
           ((or null (eql t) simple-bit-vector) result-vector)
           ((mod #.array-total-size-limit) delta)
           ((or null (mod #.array-total-size-limit)) end))
  (setq result-vector
        (etypecase result-vector
          (null (make-array (length bit-vector) :element-type 'bit :initial-element 0))
          ((eql t) bit-vector)
          (simple-bit-vector result-vector)))
  (setq end (or end (length bit-vector)))
  (assert (<= end (length bit-vector)))
  (replace result-vector bit-vector :start1 (min (length result-vector) delta)
                                    :start2 0 :end2 end)
  (bit-fill! result-vector 0 0 (min delta (length result-vector))))

(declaim (ftype (function * (values simple-bit-vector &optional)) bit-rshift))
(defun bit-rshift (bit-vector delta &optional result-vector)
  "Right-shifts BIT-VECTOR by DELTA bits and fills the new bits with zero.
The behaviour is the same as the bit-wise operations in ANSI CL: The result is
copied to RESULT-VECTOR; if it is T, BIT-VECTOR is destructively modified; if it
is NIL, a new bit-vector of the same length is created.

Note that here `right' means the direction from a larger index to a smaller one
and is contrary to the `visual' direction: i.e. (bit-rshift #*1011000 2) |->
#*1100000."
  (declare (optimize (speed 3))
           (simple-bit-vector bit-vector)
           ((or null (eql t) simple-bit-vector) result-vector)
           ((mod #.array-total-size-limit) delta))
  (setq result-vector
        (etypecase result-vector
          (null (make-array (length bit-vector) :element-type 'bit :initial-element 0))
          ((eql t) bit-vector)
          (simple-bit-vector result-vector)))
  (replace result-vector bit-vector :start2 (min delta (length bit-vector)))
  (bit-fill! result-vector 0
             (min (max 0 (- (length bit-vector) delta)) (length result-vector))))

;; not tested
(declaim (ftype (function * (values simple-bit-vector &optional)) bit-shift))
(defun bit-shift (bit-vector delta &optional result-vector)
  (declare (optimize (speed 3))
           (simple-bit-vector bit-vector)
           ((or null (eql t) simple-bit-vector) result-vector)
           ((integer #.(- array-total-size-limit) #.array-total-size-limit) delta))
  (if (>= delta 0)
      (bit-lshift bit-vector delta result-vector)
      (bit-rshift bit-vector (- delta) result-vector)))

;; (defun bit-rotate (bit-vector delta &optional result-vector)
;;   (declare (optimize (speed 3))
;;            ((mod #.array-total-size-limit) delta)
;;            (simple-bit-vector bit-vector)
;;            ((or null simple-bit-vector) result-vector))
;;   (assert (not (eql bit-vector result-vector)))
;;   (let* ((end (length bit-vector))
;;          (result-vector (or result-vector (make-array end :element-type 'bit)))
;;          (delta (mod delta end)))
;;     :unfinished))

(defpackage :cp/tzcount
  (:use :cl)
  (:export #:tzcount))
(in-package :cp/tzcount)

(declaim (inline tzcount))
(defun tzcount (x)
  "Returns the number of trailing zero bits. Note that (TZCOUNT 0) = -1."
  (- (integer-length (logand x (- x))) 1))

(defpackage :cp/modify-macro
  (:use :cl)
  (:export #:minf #:maxf #:mulf #:divf #:iorf #:xorf #:andf))
(in-package :cp/modify-macro)

(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

;; BEGIN_USE_PACKAGE
(eval-when (:compile-toplevel :load-toplevel :execute)
  (use-package :cp/modify-macro :cl-user))
(eval-when (:compile-toplevel :load-toplevel :execute)
  (use-package :cp/tzcount :cl-user))
(eval-when (:compile-toplevel :load-toplevel :execute)
  (use-package :cp/bit-basher :cl-user))
(eval-when (:compile-toplevel :load-toplevel :execute)
  (use-package :cp/read-fixnum :cl-user))
(in-package :cl-user)

;;;
;;; Body
;;;

(defun main ()
  (declare #.*opt*)
  (let* ((n (read))
         (table (make-array #.(* 20000 1024) :element-type 'bit :initial-element 0))
         (tmp (make-array #.(* 20000 1024) :element-type 'bit :initial-element 0))
         (mask (make-array 1024 :element-type 'bit :initial-element 0)))
    (declare (uint31 n)
             (simple-bit-vector table tmp))
    (setf (aref table 0) 1)
    (write-string
     (with-output-to-string (*standard-output* nil :element-type 'base-char)
       (dotimes (i n)
         (fill mask 0)
         (let* ((a (read-fixnum)))
           (declare (uint31 a))
           (bit-lshift table a tmp (min (length tmp) (* (+ i 1) 1024)))
           (dotimes (j (+ i 1))
             (let ((base (* j 16)))
               (loop for k below 16
                     do (iorf (sb-kernel:%vector-raw-bits mask k)
                              (sb-kernel:%vector-raw-bits table (+ base k))))))
           (dotimes (x (length mask))
             (when (= 1 (aref mask x))
               (iorf (aref tmp (logand x a)) 1)))
           (println (bit-count tmp 0 (min (length tmp) (* 1024 (+ i 2)))))
           (bit-fill! table 0 0 (* (+ i 1) 1024))
           (rotatef table tmp)))))))

#-swank (main)

;;;
;;; Test
;;;

#+swank
(progn
  (defparameter *lisp-file-pathname* (uiop:current-lisp-file-pathname))
  (setq *default-pathname-defaults* (uiop:pathname-directory-pathname *lisp-file-pathname*))
  (uiop:chdir *default-pathname-defaults*)
  (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *lisp-file-pathname*))
  (defparameter *problem-url* "https://yukicoder.me/problems/no/1443"))

#+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)))

#+(and sbcl (not swank))
(eval-when (:compile-toplevel)
  (when sb-c::*undefined-warnings*
    (error "undefined warnings: ~{~A~^ ~}" sb-c::*undefined-warnings*)))

;; To run: (5am:run! :sample)
#+swank
(5am:test :sample
  (5am:is
   (equal "2
3
"
          (run "2
1 4
" nil)))
  (5am:is
   (equal "2
4
5
9
"
          (run "4
30 29 16 26
" nil)))
  (5am:is
   (equal "2
4
6
6
9
17
22
34
60
63
"
          (run "10
26 7 10 0 10 30 21 30 31 20
" nil))))
0