結果

問題 No.1036 Make One With GCD 2
ユーザー sansaquasansaqua
提出日時 2020-04-24 23:16:16
言語 Common Lisp
(sbcl 2.3.8)
結果
TLE  
実行時間 -
コード長 9,439 bytes
コンパイル時間 1,556 ms
コンパイル使用メモリ 71,136 KB
実行使用メモリ 114,368 KB
最終ジャッジ日時 2024-11-07 03:06:27
合計ジャッジ時間 16,485 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 520 ms
114,368 KB
testcase_01 AC 815 ms
107,540 KB
testcase_02 AC 502 ms
107,416 KB
testcase_03 AC 66 ms
56,148 KB
testcase_04 AC 116 ms
77,788 KB
testcase_05 AC 11 ms
28,748 KB
testcase_06 AC 10 ms
26,792 KB
testcase_07 AC 715 ms
62,156 KB
testcase_08 AC 584 ms
53,972 KB
testcase_09 AC 1,849 ms
105,372 KB
testcase_10 AC 1,713 ms
99,352 KB
testcase_11 AC 1,887 ms
101,120 KB
testcase_12 AC 1,732 ms
103,528 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 07 NOV 2024 03:06:08 AM):

; file: /home/judge/data/code/Main.lisp
; in: DEFUN MAIN
;     (GCD VALUE A)
; 
; note: unable to
;   optimize
; due to type uncertainty:
;   The first argument is a (UNSIGNED-BYTE 62), not a (OR
;                                                      (INTEGER
;                                                       -4611686018427387904 -1)
;                                                      (INTEGER 1
;                                                       4611686018427387903)).
;   The second argument is a (UNSIGNED-BYTE 62), not a (OR
;                                                       (INTEGER
;                                                        -4611686018427387904 -1)
;                                                       (INTEGER 1
;                                                        4611686018427387903)).

;     (DST-QUERY DTABLE L R 0)
; --> BLOCK IF LET 
; ==>
;   (GCD (AREF TABLE H LEFT) (AREF TABLE H RIGHT))
; 
; note: unable to
;   optimize
; due to type uncertainty:
;   The first argument is a (UNSIGNED-BYTE 62), not a (OR
;                                                      (INTEGER
;                                                       -4611686018427387904 -1)
;                                                      (INTEGER 1
;                                                       4611686018427387903)).
;   The second argument is a (UNSIGNED-BYTE 62), not a (OR
;                                                       (INTEGER
;                                                        -4611686018427387904 -1)
;                                                       (INTEGER 1
;                                                        4611686018427387903)).

;     (MAKE-DISJOINT-SPARSE-TABLE AS)
; --> BLOCK LET* DO BLOCK LET TAGBODY TAGBODY LET* DO BLOCK LET TAGBODY TAGBODY 
; --> LET LET DO BLOCK LET TAGBODY TAGBODY SETF LET* 
; ==>
;   (GCD (AREF TABLE I 

ソースコード

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
(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 fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (declare #.OPT)
  (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))))))))

;;;
;;; Disjoint sparse table on arbitrary semigroup
;;;

;;; Reference:
;;; https://discuss.codechef.com/questions/117696/tutorial-disjoint-sparse-table
;;; http://noshi91.hatenablog.com/entry/2018/05/08/183946 (Japanese)
;;; http://drken1215.hatenablog.com/entry/2018/09/08/162600 (Japanese)

;; NOTE: This constructor is slow on SBCL version earlier than 1.5.6 as the type
;; propagation of MAKE-ARRAY doesn't work. The following files are required to
;; enable the optimization.
;; version < 1.5.0: array-element-type.lisp, make-array-header.lisp
;; version < 1.5.6: make-array-header.lisp
(declaim (inline make-disjoint-sparse-table))
(defun make-disjoint-sparse-table (vector)
  "BINOP := binary operator (comprising a semigroup)"
  (declare ((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)
                    (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)
                      (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 * (* *)) 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)))
        (gcd (aref table h left)
             (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 solve (n i j)
;;   (let* ((stage (min i j (- n i 1) (- n j 1)))
;;          (sector (cond ((= i stage) 0)
;;                        ((= (- n j 1) stage) 1)
;;                        ((= (- n i 1) stage) 2)
;;                        ((= j stage) 3)
;;                        (t (error "Huh?"))))
;;          (base (- (expt n 2) (expt (- n (* 2 stage)) 2)))
;;          (len (- n (* 2 stage) 1))
;;          (actual-base (+ base (* sector len)))
;;          (origin-y (ecase sector
;;                      ((0 1) stage)
;;                      ((2 3) (- n stage 1))))
;;          (origin-x (ecase sector
;;                      ((0 3) stage)
;;                      ((1 2) (- n stage 1)))))
;;     (dbg stage sector base len actual-base origin-y origin-x)
;;     (ecase sector
;;       (0 (+ actual-base (- j origin-x)))
;;       (1 (+ actual-base (- i origin-y)))
;;       (2 (+ actual-base (- origin-x j)))
;;       (3 (+ actual-base (- origin-y i))))))

(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 r 0)))
          (declare (uint62 value))
          #>value
          (loop (when (= r n)
                  (return))
                (let* ((a (aref as r))
                       (new-value (gcd value a)))
                  (when (= 1 new-value)
                    (return))
                  (setq value new-value)
                  (incf r)))
          (dbg l 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 "")))

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