提出 #7911340


ソースコード 拡げる

;; -*- 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
;;;
;;; Arithmetic operations with static modulus
;;;

(defmacro define-mod-operations (divisor)
  `(progn
     (defun mod* (&rest args)
       (reduce (lambda (x y) (mod (* x y) ,divisor)) args))

     (sb-c:define-source-transform mod* (&rest args)
       (if (null args)
           1
           (reduce (lambda (x y) `(mod (* ,x ,y) ,',divisor)) args)))

     (defun mod+ (&rest args)
       (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)))))

;;;
;;; Binomial coefficient with mod
;;; build: O(n)
;;; query: O(1)
;;;

;; TODO: non-global handling

(defconstant +binom-size+ 210000)
(defconstant +binom-mod+ #.(+ (expt 10 9) 7))

(declaim ((simple-array (unsigned-byte 32) (*)) *fact* *fact-inv* *inv*))
(defparameter *fact* (make-array +binom-size+ :element-type '(unsigned-byte 32))
  "table of factorials")
(defparameter *fact-inv* (make-array +binom-size+ :element-type '(unsigned-byte 32))
  "table of inverses of factorials")
(defparameter *inv* (make-array +binom-size+ :element-type '(unsigned-byte 32))
  "table of inverses of non-negative integers")

(defun initialize-binom ()
  (declare (optimize (speed 3) (safety 0)))
  (setf (aref *fact* 0) 1
        (aref *fact* 1) 1
        (aref *fact-inv* 0) 1
        (aref *fact-inv* 1) 1
        (aref *inv* 1) 1)
  (loop for i from 2 below +binom-size+
        do (setf (aref *fact* i) (mod (* i (aref *fact* (- i 1))) +binom-mod+)
                 (aref *inv* i) (- +binom-mod+
                                   (mod (* (aref *inv* (rem +binom-mod+ i))
                                           (floor +binom-mod+ i))
                                        +binom-mod+))
                 (aref *fact-inv* i) (mod (* (aref *inv* i)
                                             (aref *fact-inv* (- i 1)))
                                          +binom-mod+))))

(initialize-binom)

(declaim (inline binom))
(defun binom (n k)
  "Returns nCk."
  (if (or (< n k) (< n 0) (< k 0))
      0
      (mod (* (aref *fact* n)
              (mod (* (aref *fact-inv* k) (aref *fact-inv* (- n k))) +binom-mod+))
           +binom-mod+)))

(declaim (inline perm))
(defun perm (n k)
  "Returns nPk."
  (if (or (< n k) (< n 0) (< k 0))
      0
      (mod (* (aref *fact* n) (aref *fact-inv* (- n k))) +binom-mod+)))

;; TODO: compiler macro or source-transform
(declaim (inline multinomial))
(defun multinomial (&rest ks)
  "Returns the multinomial coefficient K!/k_1!k_2!...k_n! for K = k_1 + k_2 +
... + k_n. K must be equal to or smaller than
MOST-POSITIVE-FIXNUM. (multinomial) returns 1."
  (let ((sum 0)
        (result 1))
    (declare ((integer 0 #.most-positive-fixnum) result sum))
    (dolist (k ks)
      (incf sum k)
      (setq result
            (mod (* result (aref *fact-inv* k)) +binom-mod+)))
    (mod (* result (aref *fact* sum)) +binom-mod+)))

(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))
         (m (read))
         (complement 0))
    (declare (uint31 n m complement))
    ;; prefix = AB
    (loop for d from (ceiling (- n 1) 2) to n
          for y = (- n 1 d)
          do (incfmod complement (mod* (binom (- m 1) d)
                                       (binom d y))))
    ;; prefix = AA
    (loop for d from (ceiling (- n 2) 2) to n
          for y = (- n 2 d)
          do (incfmod complement (mod* (binom (- m 1) d)
                                       (binom d y))))
    (println
     (mod (- (binom (+ n m -2) (- n 1)) complement)
          +mod+))))

#-swank (main)

提出情報

提出日時
問題 E - 連呼
ユーザ sansaqua
言語 Common Lisp (SBCL 1.1.14)
得点 500
コード長 5406 Byte
結果 AC
実行時間 306 ms
メモリ 41188 KiB

ジャッジ結果

セット名 Sample ALL
得点 / 配点 0 / 0 500 / 500
結果
AC × 4
AC × 49
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
ALL hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 306 ms 41188 KiB
hand_02.txt AC 143 ms 29152 KiB
hand_03.txt AC 142 ms 29156 KiB
hand_04.txt AC 143 ms 29152 KiB
hand_05.txt AC 144 ms 29156 KiB
random_01.txt AC 143 ms 29160 KiB
random_02.txt AC 142 ms 29152 KiB
random_03.txt AC 142 ms 29152 KiB
random_04.txt AC 143 ms 29156 KiB
random_05.txt AC 142 ms 29156 KiB
random_06.txt AC 142 ms 29156 KiB
random_07.txt AC 143 ms 29160 KiB
random_08.txt AC 143 ms 29156 KiB
random_09.txt AC 143 ms 29156 KiB
random_10.txt AC 142 ms 29156 KiB
random_11.txt AC 143 ms 29156 KiB
random_12.txt AC 143 ms 29152 KiB
random_13.txt AC 143 ms 29152 KiB
random_14.txt AC 143 ms 29156 KiB
random_15.txt AC 143 ms 29156 KiB
random_16.txt AC 143 ms 29156 KiB
random_17.txt AC 143 ms 29152 KiB
random_18.txt AC 143 ms 29156 KiB
random_19.txt AC 142 ms 29160 KiB
random_20.txt AC 143 ms 29160 KiB
random_21.txt AC 145 ms 29152 KiB
random_22.txt AC 144 ms 29152 KiB
random_23.txt AC 143 ms 29156 KiB
random_24.txt AC 144 ms 29156 KiB
random_25.txt AC 144 ms 29152 KiB
random_26.txt AC 142 ms 29156 KiB
random_27.txt AC 143 ms 29156 KiB
random_28.txt AC 143 ms 29152 KiB
random_29.txt AC 142 ms 29152 KiB
random_30.txt AC 142 ms 29160 KiB
random_31.txt AC 142 ms 29152 KiB
random_32.txt AC 143 ms 29156 KiB
random_33.txt AC 142 ms 29156 KiB
random_34.txt AC 143 ms 29160 KiB
random_35.txt AC 143 ms 29156 KiB
random_36.txt AC 142 ms 29156 KiB
random_37.txt AC 143 ms 29152 KiB
random_38.txt AC 143 ms 29156 KiB
random_39.txt AC 143 ms 29156 KiB
random_40.txt AC 142 ms 29156 KiB
sample_01.txt AC 142 ms 29152 KiB
sample_02.txt AC 142 ms 29156 KiB
sample_03.txt AC 142 ms 29156 KiB
sample_04.txt AC 143 ms 29156 KiB