結果
問題 | No.40 多項式の割り算 |
ユーザー | sansaqua |
提出日時 | 2020-03-28 06:10:22 |
言語 | Common Lisp (sbcl 2.5.0) |
結果 |
AC
|
実行時間 | 13 ms / 5,000 ms |
コード長 | 10,932 bytes |
コンパイル時間 | 290 ms |
コンパイル使用メモリ | 59,100 KB |
実行使用メモリ | 28,824 KB |
最終ジャッジ日時 | 2025-01-02 10:39:23 |
合計ジャッジ時間 | 1,911 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 8 ms
24,552 KB |
testcase_01 | AC | 9 ms
24,556 KB |
testcase_02 | AC | 9 ms
24,520 KB |
testcase_03 | AC | 8 ms
24,556 KB |
testcase_04 | AC | 11 ms
24,776 KB |
testcase_05 | AC | 11 ms
22,504 KB |
testcase_06 | AC | 13 ms
28,824 KB |
testcase_07 | AC | 12 ms
24,644 KB |
testcase_08 | AC | 8 ms
24,520 KB |
testcase_09 | AC | 10 ms
22,632 KB |
testcase_10 | AC | 12 ms
22,760 KB |
testcase_11 | AC | 10 ms
22,628 KB |
testcase_12 | AC | 10 ms
28,568 KB |
testcase_13 | AC | 12 ms
24,780 KB |
testcase_14 | AC | 9 ms
22,632 KB |
testcase_15 | AC | 10 ms
24,680 KB |
testcase_16 | AC | 13 ms
24,548 KB |
testcase_17 | AC | 10 ms
24,420 KB |
testcase_18 | AC | 11 ms
22,632 KB |
testcase_19 | AC | 12 ms
24,812 KB |
testcase_20 | AC | 10 ms
22,628 KB |
testcase_21 | AC | 10 ms
26,692 KB |
testcase_22 | AC | 8 ms
22,508 KB |
testcase_23 | AC | 10 ms
22,508 KB |
testcase_24 | AC | 12 ms
24,684 KB |
testcase_25 | AC | 7 ms
22,376 KB |
testcase_26 | AC | 7 ms
22,500 KB |
testcase_27 | AC | 9 ms
26,528 KB |
testcase_28 | AC | 7 ms
22,380 KB |
testcase_29 | AC | 8 ms
22,504 KB |
testcase_30 | AC | 8 ms
22,376 KB |
testcase_31 | AC | 8 ms
22,508 KB |
testcase_32 | AC | 8 ms
22,508 KB |
testcase_33 | AC | 7 ms
22,376 KB |
testcase_34 | AC | 8 ms
24,424 KB |
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 02 JAN 2025 10:39:21 AM): ; wrote /home/judge/data/code/Main.fasl ; compilation finished in 0:00:00.181
ソースコード
(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 ;; enclose the form with VALUES to avoid being captured by LOOP macro #\# #\> (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 (declaim (inline println-sequence)) (defun println-sequence (sequence &key (out *standard-output*) (key #'identity)) (let ((init t)) (sequence:dosequence (x sequence) (if init (setq init nil) (write-char #\ out)) (princ (funcall key x) out)) (terpri out))) ;; NOTE: These are poor man's utilities for polynomial arithmetic. NOT ;; sufficiently equipped in all senses. (declaim (inline poly-value)) (defun poly-value (poly x modulus) "Returns the value f(x)." (declare (vector poly)) (let ((x^i 1) (res 0)) (declare (fixnum x^i res)) (dotimes (i (length poly)) (setq res (mod (+ res (* x^i (aref poly i))) modulus)) (setq x^i (mod (* x^i x) modulus))) res)) ;; naive multiplication in O(n^2) (declaim (inline poly-mult)) (defun poly-mult (u v modulus &optional result-vector) "Multiplies u(x) and v(x) over Z/nZ in O(deg(u)deg(v)) time. The result is stored in RESULT-VECTOR if it is given, otherwise a new vector is created." (declare (vector u v) ((or null vector) result-vector) ((integer 1 #.most-positive-fixnum) modulus)) (let* ((deg1 (loop for i from (- (length u) 1) downto 0 while (zerop (aref u i)) finally (return i))) (deg2 (loop for i from (- (length v) 1) downto 0 while (zerop (aref v i)) finally (return i))) (len (max 0 (+ deg1 deg2 1))) (res (or result-vector (make-array len :element-type (array-element-type u))))) (declare ((integer -1 (#.array-total-size-limit)) deg1 deg2 len)) (dotimes (d len res) ;; 0 <= i <= deg1, 0 <= j <= deg2 (loop with coef of-type (integer 0 #.most-positive-fixnum) = 0 for i from (max 0 (- d deg2)) to (min d deg1) for j = (- d i) do (setq coef (mod (+ coef (* (aref u i) (aref v j))) modulus)) finally (setf (aref res d) coef))))) (declaim (ftype (function * (values (mod #.most-positive-fixnum) &optional)) %mod-inverse)) (defun %mod-inverse (a modulus) "Solves ax ≡ 1 mod m. A and M must be coprime." (declare (optimize (speed 3)) (integer a) ((integer 1 #.most-positive-fixnum) modulus)) (labels ((%gcd (a b) (declare (optimize (safety 0)) ((integer 0 #.most-positive-fixnum) a b)) (if (zerop b) (values 1 0) (multiple-value-bind (p q) (floor a b) ; a = pb + q (multiple-value-bind (v u) (%gcd b q) (declare (fixnum u v)) (values u (the fixnum (- v (the fixnum (* p u)))))))))) (mod (%gcd (mod a modulus) modulus) modulus))) ;; naive division in O(n^2) ;; Reference: http://web.cs.iastate.edu/~cs577/handouts/polydivide.pdf (declaim (inline poly-floor!)) (defun poly-floor! (u v modulus &optional quotient) "Returns the quotient q(x) and the remainder r(x) over Z/nZ: u(x) = q(x)v(x) + r(x), deg(r) < deg(v). This function destructively modifies U. The time complexity is O((deg(u)-deg(v))deg(v)). The quotient is stored in QUOTIENT if it is given, otherwise a new vector is created. Note that MODULUS and V[deg(V)] must be coprime." (declare (vector u v) ((integer 1 #.most-positive-fixnum) modulus)) ;; m := deg(u), n := deg(v) (let* ((m (loop for i from (- (length u) 1) downto 0 while (zerop (aref u i)) finally (return i))) (n (loop for i from (- (length v) 1) downto 0 unless (zerop (aref v i)) do (return i) finally (error 'division-by-zero :operation #'poly-floor! :operands (list u v)))) (quot (or quotient (make-array (max 0 (+ 1 (- m n))) :element-type (array-element-type u)))) ;; FIXME: Is it better to signal an error in non-coprime case? (inv (%mod-inverse (aref v n) modulus))) (declare ((integer -1 (#.array-total-size-limit)) m n)) (loop for k from (- m n) downto 0 do (setf (aref quot k) (mod (* (aref u (+ n k)) inv) modulus)) (loop for j from (+ n k -1) downto k do (setf (aref u j) (mod (- (aref u j) (* (aref quot k) (aref v (- j k)))) modulus)))) (loop for i from (- (length u) 1) downto n do (setf (aref u i) 0) finally (return (values quot u))))) ;; naive division in O(n^2) (defun poly-mod! (poly divisor modulus) "Returns the remainder of POLY divided by DIVISOR over Z/nZ. This function destructively modifies POLY." (declare (vector poly divisor) ((integer 1 #.most-positive-fixnum) modulus)) (let* ((m (loop for i from (- (length poly) 1) downto 0 while (zerop (aref poly i)) finally (return i))) (n (loop for i from (- (length divisor) 1) downto 0 unless (zerop (aref divisor i)) do (return i) finally (error 'division-by-zero :operation #'poly-mod! :operands (list poly divisor)))) (inv (%mod-inverse (aref divisor n) modulus))) (declare ((integer -1 (#.array-total-size-limit)) m n)) (loop for pivot-deg from m downto n for factor of-type (integer 0 #.most-positive-fixnum) = (mod (* (aref poly pivot-deg) inv) modulus) do (loop for delta from 0 to n do (setf (aref poly (- pivot-deg delta)) (mod (- (aref poly (- pivot-deg delta)) (* factor (aref divisor (- n delta)))) modulus)))) poly)) (defun poly-power (poly exponent divisor modulus) "Returns POLY to the power of EXPONENT modulo DIVISOR over Z/nZ." (declare (vector poly divisor) ((integer 0 #.most-positive-fixnum) exponent) ((integer 1 #.most-positive-fixnum) modulus)) (labels ((recur (power) (declare ((integer 0 #.most-positive-fixnum) power)) (cond ((zerop power) (make-array 1 :element-type (array-element-type poly) :initial-element 1)) ((oddp power) (poly-mod! (poly-mult poly (recur (- power 1)) modulus) divisor modulus)) ((let ((res (recur (floor power 2)))) (poly-mod! (poly-mult res res modulus) divisor modulus)))))) (recur exponent))) (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 ;;; (defun main () (let* ((d (read)) (as (make-array (+ d 1) :element-type 'int32))) (dotimes (i (+ d 1)) (setf (aref as i) (read))) (let* ((res (poly-mod! as #(0 -1 0 1) +mod+)) (max-d (position 0 res :from-end t :test-not #'=))) (if max-d (progn (println max-d) (println-sequence (subseq res 0 (+ max-d 1)) :key (lambda (x) (if (> x 500000000) (- x +mod+) x)))) (format t "0~%0~%"))))) #-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 "5 1 1 1 0 0 1 " "2 1 2 1 ")) (it.bese.fiveam:is (common-lisp-user::io-equal "8 0 -5 0 4 0 1 -1 0 1 " "0 0 ")) (it.bese.fiveam:is (common-lisp-user::io-equal "5 -5 0 -1 1 1 1 " "1 -5 2 ")))