結果
問題 | No.915 Plus Or Multiple Operation |
ユーザー | sansaqua |
提出日時 | 2019-10-25 23:04:17 |
言語 | Common Lisp (sbcl 2.3.8) |
結果 |
WA
|
実行時間 | - |
コード長 | 16,191 bytes |
コンパイル時間 | 929 ms |
コンパイル使用メモリ | 56,524 KB |
実行使用メモリ | 27,176 KB |
最終ジャッジ日時 | 2024-11-07 03:37:39 |
合計ジャッジ時間 | 1,629 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | AC | 9 ms
22,400 KB |
testcase_02 | AC | 9 ms
22,528 KB |
testcase_03 | AC | 10 ms
22,656 KB |
testcase_04 | AC | 10 ms
22,528 KB |
testcase_05 | AC | 9 ms
22,528 KB |
testcase_06 | AC | 9 ms
22,528 KB |
testcase_07 | AC | 9 ms
22,528 KB |
testcase_08 | AC | 9 ms
22,528 KB |
testcase_09 | AC | 10 ms
22,528 KB |
testcase_10 | AC | 9 ms
22,528 KB |
testcase_11 | AC | 9 ms
22,528 KB |
testcase_12 | WA | - |
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 07 NOV 2024 03:37:36 AM): ; file: /home/judge/data/code/Main.lisp ; in: DEFUN BIT-LSHIFT ; (LDB (BYTE (- 64 D%64) 0) -1) ; ; note: forced to do full call ; unable to do inline ASH (cost 3) because: ; The result is a (VALUES (INTEGER -18446744073709551616 -2) &OPTIONAL), not a (VALUES ; FIXNUM ; &OPTIONAL). ; unable to do inline ASH (cost 4) because: ; The result is a (VALUES (INTEGER -18446744073709551616 -2) &OPTIONAL), not a (VALUES ; (SIGNED-BYTE ; 64) ; &OPTIONAL). ; etc. ; ; note: forced to do full call ; unable to do inline fixnum arithmetic (cost 1) because: ; The first argument is a (INTEGER -18446744073709551616 -2), not a FIXNUM. ; The result is a (VALUES (INTEGER 1 18446744073709551615) &OPTIONAL), not a (VALUES ; FIXNUM ; &OPTIONAL). ; unable to do inline (signed-byte 64) arithmetic (cost 2) because: ; The first argument is a (INTEGER -18446744073709551616 -2), not a (SIGNED-BYTE ; 64). ; The result is a (VALUES (INTEGER 1 18446744073709551615) &OPTIONAL), not a (VALUES ; (SIGNED-BYTE ; 64) ;
ソースコード
;; -*- coding: utf-8 -*- (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 #\# #\> (lambda (s c p) (declare (ignore c p)) (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 ;;; ;;; Complement to the bitwise operations in CLHS ;;; (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+ #.(- (ash 1 64) 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) ((integer 0 #.most-positive-fixnum) start) ((or null (integer 0 #.most-positive-fixnum)) 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) ;; (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 transformer ;; 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) ((integer 0 #.most-positive-fixnum) start) ((or null (integer 0 #.most-positive-fixnum)) 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 ((integer 0 #.most-positive-fixnum) 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))))) ;; unfinished ;; (defun bit-shift (bit-vector delta &optional result-vector) ;; "Shifts BIT-VECTOR by DELTA bits and fills the new bits with zero. Positive ;; DELTA means left-shifting and negative DELTA means right-shifting. ;; The behaviour is the same as the bit-wise operations in CLHS: 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." ;; (declare (simple-bit-vector bit-vector) ;; ((or null (eql t) simple-bit-vector) result-vector) ;; (fixnum 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))) ;; (when (>= delta 0) ;; (return-from bit-shift (bit-lshift bit-vector delta result-vector))) ;; (let* ((delta (- delta)) ;; (end (length bit-vector))) ;; (unless (zerop end) ;; (multiple-value-bind (d/64 d%64) (floor delta 64) ;; (multiple-value-bind (end/64 end%64) (floor end 64) ;; ;; process the initial word separately ;; (when (and (> d%64 0) (< d/64 (ceiling end 64))) ;; (setf (ldb (byte (- 64 d%64) 0) ;; (sb-kernel:%vector-raw-bits result-vector 0)) ;; (ldb (byte (- 64 d%64) d%64) ;; (sb-kernel:%vector-raw-bits bit-vector d/64)))) ;; (do ((i (ceiling delta 64) (+ i 1))) ;; ((>= i end/64)) ;; (setf (ldb (byte d%64 (- 64 d%64)) ;; (sb-kernel:%vector-raw-bits result-vector (- i d/64 1))) ;; (ldb (byte d%64 0) ;; (sb-kernel:%vector-raw-bits bit-vector i))) ;; (setf (ldb (byte (- 64 d%64) 0) ;; (sb-kernel:%vector-raw-bits result-vector (- i d/64))) ;; (ldb (byte (- 64 d%64) d%64) ;; (sb-kernel:%vector-raw-bits bit-vector i)))) ;; ;; process the last word separately ;; (unless (zerop end%64) ;; (setf (ldb (byte d%64 (- 64 d%64)) ;; (sb-kernel:%vector-raw-bits result-vector (- end/64 d/64 1))) ;; (ldb (byte (min d%64 end%64) 0) ;; (sb-kernel:%vector-raw-bits bit-vector end/64))) ;; (setf (ldb (byte (- 64 d%64) 0) ;; (sb-kernel:%vector-raw-bits result-vector (- end/64 d/64))) ;; (ldb (byte (max 0 (- end%64 d%64)) d%64) ;; (sb-kernel:%vector-raw-bits bit-vector end/64))))))) ;; result-vector)) ;; TODO: right shift (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 CLHS: 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, 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) ((integer 0 #.most-positive-fixnum) delta) ((or null (integer 0 #.most-positive-fixnum)) 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))) (setq end (min end (max 0 (- (length result-vector) delta)))) (multiple-value-bind (d/64 d%64) (floor delta 64) (declare (optimize (safety 0)) (simple-bit-vector result-vector)) (multiple-value-bind (end/64 end%64) (floor end 64) ;; process the last word separately (unless (zerop end%64) (let ((word (sb-kernel:%vector-raw-bits bit-vector end/64))) (setf (sb-kernel:%vector-raw-bits result-vector (+ end/64 d/64)) (u64-dpb word (byte (min end%64 (- 64 d%64)) d%64) (sb-kernel:%vector-raw-bits result-vector (+ end/64 d/64)))) (when (> end%64 (- 64 d%64)) (setf (ldb (byte (- end%64 (- 64 d%64)) 0) (sb-kernel:%vector-raw-bits result-vector (+ 1 end/64 d/64))) (ldb (byte (- end%64 (- 64 d%64)) (- 64 d%64)) word))))) ;; Body. We avoid LDB and DPB here for efficiency, though this seems to ;; be somewhat incomprehensible... (let* ((mask0 (ldb (byte 64 0) (lognot (ldb (byte d%64 0) -1)))) (mask1-lo (ldb (byte (- 64 d%64) 0) -1)) (mask1-hi (ldb (byte 64 0) (lognot (ash mask1-lo d%64))))) (declare ((unsigned-byte 64) mask0 mask1-lo mask1-hi)) (do ((i (- end/64 1) (- i 1))) ((< i 0)) (let ((word (sb-kernel:%vector-raw-bits bit-vector i)) (i+d/64 (+ i d/64))) (declare ((unsigned-byte 64) word) ((mod #.most-positive-fixnum) i+d/64)) (setf (sb-kernel:%vector-raw-bits result-vector i+d/64) (logior (the (unsigned-byte 64) (ash (logand word mask1-lo) d%64)) (logand (sb-kernel:%vector-raw-bits result-vector i+d/64) mask1-hi))) (setf (sb-kernel:%vector-raw-bits result-vector (+ 1 i+d/64)) (logior (ash word (- d%64 64)) (logand (sb-kernel:%vector-raw-bits result-vector (+ 1 i+d/64)) mask0)))))) ;; zero padding (when (< d/64 (ceiling (length result-vector) 64)) (setf (ldb (byte d%64 0) (sb-kernel:%vector-raw-bits result-vector d/64)) 0)) ;; REVIEW: May we set the last word of a bit vector to zero beyond the ;; actual bound? (dotimes (i (min d/64 (ceiling (length result-vector) 64))) (setf (sb-kernel:%vector-raw-bits result-vector i) 0)) result-vector))) ;; We must implement the right shift beforehand. ;; (defun bit-rotate (bit-vector delta &optional result-vector) ;; (declare (optimize (speed 3)) ;; ((integer 0 #.most-positive-fixnum) 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)) (defun bench (size sample) (declare ((unsigned-byte 32) size sample)) (let ((seq (make-array size :element-type 'bit)) (state (sb-ext:seed-random-state 0))) (gc :full t) (time (loop repeat sample sum (aref (bit-lshift seq (random 128 state)) 0) of-type bit)))) (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 ;;; (defun solve (a b c) (when (= c 1) (println -1) (return-from solve)) (sb-int:named-let recur ((a a) (res 0)) (if (zerop a) res (if (<= a (- c 1)) (+ res b) (if (<= a (* 2 (- c 1))) (+ res b b) (multiple-value-bind (quot rem) (floor a c) (if (zerop rem) (recur quot (+ res b)) (recur (- a rem) (+ res b))))))))) (define-modify-macro minf (new-value) min) (defun test (a c) (let ((dp (make-array 200 :element-type 'uint32 :initial-element #xffffffff))) (dotimes (i c) (setf (aref dp i) 1)) (loop repeat 200 do (loop for i from 1 below (length dp) do (unless (zerop (aref dp i)) (loop for j from 1 below c while (< (+ i j) (length dp)) do (minf (aref dp (+ i j)) (+ 1 (aref dp i)))) (when (< (* i c) (length dp)) (minf (aref dp (* i c)) (+ 1 (aref dp i))))))) (let ((dp2 (make-array 200))) (setf (aref dp2 0) 1) (loop for i from 1 below (length dp) do (setf (aref dp2 i) (solve i 1 c))) (values dp dp2) (equalp dp dp2)))) (defun main () (let ((q (read))) (dotimes (_ q) (let* ((a (read)) (b (read)) (c (read))) ;; (dbg a b c) (println (solve a b c)))))) #-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 "C:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out))) #+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)))