結果

問題 No.1036 Make One With GCD 2
ユーザー sansaquasansaqua
提出日時 2020-04-25 00:52:25
言語 Common Lisp
(sbcl 2.3.8)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 8,848 bytes
コンパイル時間 1,641 ms
コンパイル使用メモリ 50,320 KB
実行使用メモリ 105,728 KB
最終ジャッジ日時 2023-10-14 19:39:43
合計ジャッジ時間 38,126 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 515 ms
103,940 KB
testcase_01 AC 1,419 ms
101,920 KB
testcase_02 AC 289 ms
104,032 KB
testcase_03 AC 54 ms
56,464 KB
testcase_04 AC 87 ms
75,280 KB
testcase_05 AC 9 ms
25,348 KB
testcase_06 AC 8 ms
23,376 KB
testcase_07 AC 435 ms
56,952 KB
testcase_08 AC 355 ms
54,312 KB
testcase_09 AC 1,138 ms
103,944 KB
testcase_10 AC 1,060 ms
95,824 KB
testcase_11 AC 1,164 ms
101,928 KB
testcase_12 AC 1,072 ms
95,852 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,921 ms
97,824 KB
testcase_16 AC 1,938 ms
95,820 KB
testcase_17 TLE -
testcase_18 AC 9 ms
23,548 KB
testcase_19 AC 12 ms
27,284 KB
testcase_20 AC 13 ms
25,944 KB
testcase_21 AC 12 ms
23,864 KB
testcase_22 AC 1,891 ms
93,688 KB
testcase_23 AC 1,393 ms
75,268 KB
testcase_24 AC 1,971 ms
97,864 KB
testcase_25 AC 1,798 ms
91,688 KB
testcase_26 AC 1,859 ms
93,728 KB
testcase_27 AC 9 ms
23,436 KB
testcase_28 AC 9 ms
25,436 KB
testcase_29 AC 9 ms
25,388 KB
testcase_30 AC 9 ms
23,292 KB
testcase_31 AC 8 ms
23,328 KB
testcase_32 AC 9 ms
23,392 KB
testcase_33 AC 9 ms
23,384 KB
testcase_34 AC 9 ms
23,332 KB
testcase_35 AC 8 ms
23,348 KB
testcase_36 AC 9 ms
23,440 KB
testcase_37 AC 9 ms
25,380 KB
testcase_38 AC 276 ms
101,924 KB
testcase_39 AC 1,063 ms
103,936 KB
testcase_40 AC 1,394 ms
75,276 KB
testcase_41 AC 904 ms
105,728 KB
testcase_42 AC 984 ms
101,912 KB
testcase_43 AC 527 ms
101,884 KB
testcase_44 AC 602 ms
102,020 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 14 OCT 2023 07:39:04 PM):
; processing (SB-INT:DEFCONSTANT-EQX OPT ...)
; processing (SET-DISPATCH-MACRO-CHARACTER #\# ...)
; processing (DISABLE-DEBUGGER)
; processing (DEFMACRO DEFINE-INT-TYPES ...)
; processing (DEFINE-INT-TYPES 2 ...)
; processing (DECLAIM (INLINE TZCOUNT))
; processing (DEFUN TZCOUNT ...)
; processing (DEF MINF ...)
; processing (DEF MAXF ...)
; processing (DEF MULF ...)
; processing (DEF DIVF ...)
; processing (DEF IORF ...)
; processing (DEF XORF ...)
; processing (DEF ANDF ...)
; processing (DECLAIM (FTYPE # ...))
; processing (DEFUN READ-FIXNUM+ ...)
; processing (DEFTYPE UINT62+ ...)
; processing (DECLAIM (INLINE FAST-GCD) ...)
; processing (DEFUN FAST-GCD ...)
; processing (DEFUN MAKE-DISJOINT-SPARSE-TABLE ...)
; processing (DECLAIM (INLINE DST-QUERY))
; processing (DEFUN DST-QUERY ...)
; processing (IN-PACKAGE :CL-USER)
; processing (DEFMACRO DBG ...)
; processing (DECLAIM (INLINE PRINTLN))
; processing (DEFUN PRINTLN ...)
; processing (DEFCONSTANT +MOD+ ...)
; processing (DEFUN MAIN ...)
; processing (MAIN)

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

ソースコード

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
           #\# #\> (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

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

;; BEGIN_INSERTED_CONTENTS
(declaim (inline tzcount))
(defun tzcount (x)
  "Is equivalent to TZCNT operation: it returns the number of trailing zero
bits. Note that (TZCOUNT 0) = -1."
  (- (integer-length (logand x (- x))) 1))

;; The following code also works though it is slower on AtCoder as LOGCOUNT is
;; not transformed to POPCNT on SBCL 1.1.14.

;; (declaim (inline tzcount2))
;; (defun tzcount2 (x)
;;   (logcount (- (logand x (- x)) 1)))

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

(declaim (ftype (function * (values uint62 &optional)) read-fixnum+))
(defun read-fixnum+ (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (declare #.OPT
           #-swank (sb-kernel:ansi-stream in))
  (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* ((result (loop (let ((byte (%read-byte)))
                           (when(<= 48 byte)
                             (return (- byte 48)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return result)))))))

(deftype uint62+ () '(integer 1 #.most-positive-fixnum))

(declaim (inline fast-gcd)
         (ftype (function * (values (integer 0 #.most-positive-fixnum) &optional)) fast-gcd))
(defun fast-gcd (u v)
  (declare (optimize (speed 3) (safety 0))
           ((integer 0 #.most-positive-fixnum) u v))
  (let ((shift (tzcount (logior u v))))
    (setq u (ash u (- 1 (integer-length (logand u (- u))))))
    (loop (setq v (ash v (- 1 (integer-length (logand v (- v))))))
          (when (> u v)
            (rotatef u v))
          (setq v (- v u))
          ;; (decf v u)
          (when (zerop v)
            (return (the (integer 1 #.most-positive-fixnum)
                         (ash u shift)))))))

(defun make-disjoint-sparse-table (vector)
  "BINOP := binary operator (comprising a semigroup)"
  (declare #.OPT
           ((simple-array uint62 (*)) vector))
  (let* ((n (length vector))
         (height (max 1 (integer-length (- n 1))))
         (table (make-array (list height n) :element-type 'uint62)))
    (dotimes (j n)
      (setf (aref table 0 j) (aref vector j)))
    (do ((i 1 (+ i 1)))
        ((>= i height))
      (let* ((width/2 (ash 1 i))
             (width (* width/2 2)))
        (do ((j 0 (+ j width)))
            ((>= j n))
          (let ((mid (min (+ j width/2) n)))
            ;; fill the first half
            (setf (aref table i (- mid 1))
                  (aref vector (- mid 1)))
            (do ((k (- mid 2) (- k 1)))
                ((< k j))
              (setf (aref table i k)
                    (fast-gcd (aref vector k) (aref table i (+ k 1)))))
            (when (>= mid n)
              (return))
            ;; fill the second half
            (setf (aref table i mid)
                  (aref vector mid))
            (let ((end (min n (+ mid width/2))))
              (do ((k (+ mid 1) (+ k 1)))
                  ((>= k end))
                (setf (aref table i k)
                      (fast-gcd (aref table i (- k 1)) (aref vector k)))))))))
    table))

(declaim (inline dst-query))
(defun dst-query (table left right &optional identity)
  "Queries the interval [LEFT, RIGHT). Returns IDENTITY for a null interval [x,
x)."
  (declare ((integer 0 #.most-positive-fixnum) left right)
           ((simple-array uint62 (* *)) table))
  (when (>= left right)
    (assert (= left right))
    (return-from dst-query identity))
  (setq right (- right 1)) ;; change to closed interval
  (if (= left right)
      (aref table 0 left)
      (let ((h (- (integer-length (logxor left right)) 1)))
        (fast-gcd (the uint62+ (aref table h left))
                  (the uint62+ (aref table h right))))))

(in-package :cl-user)

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

(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 main ()
  (declare #.OPT)
  (let* ((n (read))
         (as (make-array n :element-type 'uint62 :initial-element 0)))
    (declare (uint31 n))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum+)))
    (let ((r 0)
          (dtable (make-disjoint-sparse-table as))
          (res 0))
      (declare (uint62 r res))
      (dotimes (l n)
        (maxf r l)
        (let ((value (dst-query dtable l (max (+ l 1) r) 0)))
          (declare (uint62 value))
          (loop (when (= r n)
                  (return))
                (let* ((a (aref as r))
                       (new-value (fast-gcd value a)))
                  (when (= 1 new-value)
                    (return))
                  (setq value new-value)
                  (incf r)))
          (incf res (- r l))))
      (println (- (ash (* n (+ n 1)) -1)
                  res)))))

#-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 "500000~%")
    (dotimes (_ 500000)
      (println (+ 1 (random #.(expt 10 18))) out))))

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
1 2 3
"
    "4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4
1 1 1 1
"
    "10
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4
2 4 8 16
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "10
801754 703742 332182 68016 914814 8470 937255 293192 313080 501971
"
    "28
")))
0