Submission #13288139


Source Code Expand

(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

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Arithmetic operations with static modulus
;;;

;; FIXME: Currently MOD* and MOD+ doesn't apply MOD when the number of
;; parameters is one.
(defmacro define-mod-operations (divisor)
  `(progn
     (defun mod* (&rest args)
       (reduce (lambda (x y) (mod (* x y) ,divisor)) args))

     (defun mod+ (&rest args)
       (reduce (lambda (x y) (mod (+ x y) ,divisor)) args))

     #+sbcl
     (eval-when (:compile-toplevel :load-toplevel :execute)
       (locally (declare (muffle-conditions warning))
         (sb-c:define-source-transform mod* (&rest args)
           (if (null args)
               1
               (reduce (lambda (x y) `(mod (* ,x ,y) ,',divisor)) args)))
         (sb-c:define-source-transform mod+ (&rest args)
           (if (null args)
               0
               (reduce (lambda (x y) `(mod (+ ,x ,y) ,',divisor)) args)))))

     (define-modify-macro incfmod (delta)
       (lambda (x y) (mod (+ x y) ,divisor)))

     (define-modify-macro decfmod (delta)
       (lambda (x y) (mod (- x y) ,divisor)))

     (define-modify-macro mulfmod (multiplier)
       (lambda (x y) (mod (* x y) ,divisor)))))


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

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

(define-mod-operations +mod+)

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (s (read-line)))
    (declare (uint31 n))
    (labels ((no () (println 0) (return-from main)))
      (let ((y 0)
            (res 1))
        (declare (uint31 y res))
        (dotimes (x (* 2 n))
          (cond ((and (> y 0)
                      (or (and (char= #\W (aref s x)) (evenp y))
                          (and (char= #\B (aref s x)) (oddp y))))
                 (mulfmod res y)
                 (decf y))
                ((and (< y n)
                      (or (and (char= #\W (aref s x)) (oddp y))
                          (and (char= #\B (aref s x)) (evenp y))))
                 (incf y))
                (t (no))))
        (unless (zerop y) (no))
        (loop for x from 1 to n
              do (mulfmod res x))
        (println 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 "2
BWWB
"
    "4
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4
BWBBWWWB
"
    "288
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
WWWWWWWWWW
"
    "0
")))

Submission Info

Submission Time
Task C - Cell Inversion
User sansaqua
Language Common Lisp (SBCL 1.1.14)
Score 500
Code Size 5711 Byte
Status AC
Exec Time 196 ms
Memory 26340 KiB

Judge Result

Set Name All Sample
Score / Max Score 500 / 500 0 / 0
Status
AC × 36
AC × 3
Set Name Test Cases
All sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, 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
Sample sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
sample_01 AC 196 ms 26340 KiB
sample_02 AC 62 ms 14820 KiB
sample_03 AC 61 ms 14820 KiB
testcase_01 AC 72 ms 14820 KiB
testcase_02 AC 70 ms 14816 KiB
testcase_03 AC 79 ms 16872 KiB
testcase_04 AC 66 ms 14820 KiB
testcase_05 AC 72 ms 14824 KiB
testcase_06 AC 69 ms 14820 KiB
testcase_07 AC 76 ms 14824 KiB
testcase_08 AC 83 ms 16868 KiB
testcase_09 AC 83 ms 16872 KiB
testcase_10 AC 62 ms 14816 KiB
testcase_11 AC 67 ms 14816 KiB
testcase_12 AC 79 ms 16868 KiB
testcase_13 AC 82 ms 16868 KiB
testcase_14 AC 76 ms 14820 KiB
testcase_15 AC 81 ms 16864 KiB
testcase_16 AC 71 ms 14824 KiB
testcase_17 AC 80 ms 16868 KiB
testcase_18 AC 77 ms 14816 KiB
testcase_19 AC 78 ms 14820 KiB
testcase_20 AC 69 ms 14820 KiB
testcase_21 AC 83 ms 16868 KiB
testcase_22 AC 83 ms 16868 KiB
testcase_23 AC 72 ms 14820 KiB
testcase_24 AC 82 ms 16864 KiB
testcase_25 AC 83 ms 16868 KiB
testcase_26 AC 62 ms 14820 KiB
testcase_27 AC 62 ms 14820 KiB
testcase_28 AC 82 ms 16868 KiB
testcase_29 AC 79 ms 16868 KiB
testcase_30 AC 83 ms 16868 KiB
testcase_31 AC 79 ms 16868 KiB
testcase_32 AC 81 ms 16864 KiB
testcase_33 AC 80 ms 16868 KiB