Submission #7983566


Source Code Expand

;; -*- 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
;; Should we do this with UNWIND-PROTECT?
(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)))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (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 #\-))
                                  (setf 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
;;;

(defstruct (multidiagonal-matrix
            (:constructor make-md-matrix
                (size k
                 &aux
                 (storage (make-array (list size (+ 1 (* 2 k)))
                                      :element-type 'double-float
                                      :initial-element 0d0))))
            (:conc-name md-))
  (size 0 :type (integer 0 #.most-positive-fixnum))
  (k 0 :type (integer 0 #.most-positive-fixnum))
  (storage nil :type (simple-array double-float (* *))))

(declaim (inline md-gemv))
(defun md-gemv (md-matrix vector)
  (let* ((storage (md-storage md-matrix))
         (size (length vector))
         (k (md-k md-matrix))
         (result (make-array size
                             :element-type 'double-float
                             :initial-element 0d0)))
    (assert (= size (md-size md-matrix)))
    (dotimes (i size)
      (let ((value 0d0))
        (declare (double-float value))
        ;; VECTOR[i] corresponds to STORAGE[][K]
        (loop for j from (max 0 (- i k)) to (min (- size 1) (+ i k))
              do (incf value
                       (* (aref vector j)
                          (aref storage i (+ k (- j i))))))
        (setf (aref result i) value)))
    result))

(declaim (inline md-gemv!))
(defun md-gemv! (md-matrix vector result-vector)
  (let* ((storage (md-storage md-matrix))
         (size (length vector))
         (k (md-k md-matrix)))
    (assert (= size
               (md-size md-matrix)
               (length result-vector)))
    (dotimes (i size)
      (let ((value 0d0))
        (declare (double-float value))
        ;; VECTOR[i] corresponds to STORAGE[][K]
        (loop for j from (max 0 (- i k)) to (min (- size 1) (+ i k))
              do (incf value
                       (* (aref vector j)
                          (aref storage i (+ k (- j i))))))
        (setf (aref result-vector i) value)))
    result-vector))

(declaim (inline md-ref))
(defun md-ref (md-matrix i j)
  (let* ((storage (md-storage md-matrix))
         (k (md-k md-matrix))
         (internal-j (+ k (- j i))))
    (aref storage i internal-j)))

(declaim (inline (setf md-ref)))
(defun (setf md-ref) (new-value md-matrix i j)
  (declare ((integer 0 #.most-positive-fixnum) i j))
  (let* ((storage (md-storage md-matrix))
         (k (md-k md-matrix))
         (internal-j (+ k (- j i))))
    (setf (aref storage i internal-j) new-value)))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (m (read))
         (matrix (make-md-matrix n 10))
         (submissions (make-array n :element-type 'list :initial-element nil))
         (dp (make-array n :element-type 'double-float :initial-element 1d0))
         (tmp (make-array n :element-type 'double-float)))
    (declare (uint31 n m)
             ((simple-array double-float (*)) dp tmp))
    (dotimes (_ m)
      (let ((a (- (read-fixnum) 1))
            (b (- (read-fixnum) 1)))
        (push b (aref submissions a))))
    (dotimes (j n)
      (let ((card (length (aref submissions j))))
        (unless (zerop card)
          (let ((value (/ 0.9d0 card)))
            (dolist (i (aref submissions j))
              (setf (md-ref matrix i j) value))))))
    (dotimes (_ 1000)
      (setq tmp (md-gemv! matrix dp tmp))
      (dotimes (i n)
        (incf (aref tmp i) 0.1d0))
      (rotatef tmp dp))
    (with-buffered-stdout
      (map () #'println dp))))

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3 6
1 2
1 3
2 1
2 3
3 1
3 2
"
    "1
1
1
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3 4
1 2
1 3
2 1
3 1
"
    "1.473684210526316
0.7631578947368423
0.7631578947368423
")))

Submission Info

Submission Time
Task E - Page Rank
User sansaqua
Language Common Lisp (SBCL 1.1.14)
Score 100
Code Size 8765 Byte
Status AC
Exec Time 450 ms
Memory 27364 KiB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 25
Set Name Test Cases
All 10_sample_01.txt, 10_sample_02.txt, 20-random_small-00.txt, 20-random_small-01.txt, 20-random_small-02.txt, 20-random_small-03.txt, 20-random_small-04.txt, 20-random_small-05.txt, 20-random_small-06.txt, 20-random_small-07.txt, 20-random_small-08.txt, 20-random_small-09.txt, 30-random_large-00.txt, 30-random_large-01.txt, 30-random_large-02.txt, 30-random_large-03.txt, 30-random_large-04.txt, 30-random_large-05.txt, 30-random_large-06.txt, 30-random_large-07.txt, 30-random_large-08.txt, 30-random_large-09.txt, 40-random_max-00.txt, 40-random_max-01.txt, 40-random_max-02.txt
Case Name Status Exec Time Memory
10_sample_01.txt AC 273 ms 25824 KiB
10_sample_02.txt AC 100 ms 21092 KiB
20-random_small-00.txt AC 104 ms 21092 KiB
20-random_small-01.txt AC 103 ms 21092 KiB
20-random_small-02.txt AC 101 ms 21096 KiB
20-random_small-03.txt AC 104 ms 21096 KiB
20-random_small-04.txt AC 101 ms 21088 KiB
20-random_small-05.txt AC 102 ms 21092 KiB
20-random_small-06.txt AC 101 ms 21092 KiB
20-random_small-07.txt AC 104 ms 21092 KiB
20-random_small-08.txt AC 103 ms 21096 KiB
20-random_small-09.txt AC 103 ms 21088 KiB
30-random_large-00.txt AC 112 ms 21096 KiB
30-random_large-01.txt AC 111 ms 21088 KiB
30-random_large-02.txt AC 411 ms 27232 KiB
30-random_large-03.txt AC 312 ms 25188 KiB
30-random_large-04.txt AC 205 ms 23140 KiB
30-random_large-05.txt AC 222 ms 23136 KiB
30-random_large-06.txt AC 155 ms 21092 KiB
30-random_large-07.txt AC 207 ms 23140 KiB
30-random_large-08.txt AC 382 ms 25184 KiB
30-random_large-09.txt AC 423 ms 27240 KiB
40-random_max-00.txt AC 449 ms 27364 KiB
40-random_max-01.txt AC 449 ms 27360 KiB
40-random_max-02.txt AC 450 ms 27360 KiB