結果

問題 No.2007 Arbitrary Mod (Easy)
ユーザー n_getn_get
提出日時 2022-07-15 22:07:44
言語 Scheme
(Gauche-0.9.14)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 2,865 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 5,208 KB
実行使用メモリ 15,820 KB
最終ジャッジ日時 2023-09-10 02:04:52
合計ジャッジ時間 5,685 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
13,856 KB
testcase_01 AC 26 ms
13,992 KB
testcase_02 AC 26 ms
13,804 KB
testcase_03 AC 26 ms
14,004 KB
testcase_04 AC 25 ms
13,860 KB
testcase_05 AC 26 ms
13,928 KB
testcase_06 AC 25 ms
13,888 KB
testcase_07 AC 25 ms
13,932 KB
testcase_08 AC 26 ms
13,856 KB
testcase_09 AC 25 ms
13,864 KB
testcase_10 AC 25 ms
15,820 KB
testcase_11 AC 26 ms
13,896 KB
testcase_12 AC 26 ms
13,884 KB
testcase_13 AC 26 ms
13,820 KB
testcase_14 AC 26 ms
13,808 KB
testcase_15 AC 26 ms
13,852 KB
testcase_16 AC 26 ms
14,000 KB
testcase_17 AC 26 ms
14,040 KB
testcase_18 AC 25 ms
13,884 KB
testcase_19 AC 26 ms
14,068 KB
testcase_20 AC 26 ms
13,840 KB
testcase_21 AC 26 ms
13,808 KB
testcase_22 AC 25 ms
13,852 KB
testcase_23 AC 25 ms
13,848 KB
testcase_24 AC 25 ms
13,868 KB
testcase_25 AC 26 ms
13,868 KB
testcase_26 AC 26 ms
13,816 KB
testcase_27 AC 25 ms
13,920 KB
testcase_28 AC 26 ms
14,016 KB
testcase_29 AC 26 ms
13,916 KB
testcase_30 AC 26 ms
14,000 KB
testcase_31 AC 25 ms
13,992 KB
testcase_32 AC 26 ms
13,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

(use util.queue)(use util.match)(use gauche.collection)(use gauche.sequence)
(define-syntax def-syntax (syntax-rules()((_ v e ...)(define-syntax v (syntax-rules e ...)))))
(define-syntax def-const define-constant)(define-syntax def define)(define-syntax def-inline define-inline)
(def-syntax def-names ()((_[x y]...)(begin(define-constant x y)...)))
(def-names [// quotient][% modulo][v-make make-vector][v-ref vector-ref][v-set! vector-set!][ht-make make-hash-table][ht-ref hash-table-get][ht-set! hash-table-put!][ht-keys hash-table-keys][ht-values hash-table-values][q-make make-queue][make-q make-queue][enq! enqueue!][deq! dequeue!][q-empty? queue-empty?])
(def-syntax input (: list vec cons str)
  ((_ : cons e ...)(cons(input : e ...)(input : e ...)))((_ : list str)(input : string->list str))
  ((_ : vec str)(input : string->vector str))((_ : vec n e ...)(vector-tabulate n(^_(input : e ...))))
  ((_ : list n e ...)(let l([k n])(if(zero? k)'()(cons(input : e ...)(l(- k 1))))))((_ :)(read))((_ : : e)e)
  ((_ : str)(let1 s(read-line)(if(string=? s "")(read-line)s)))((_[[v]: e ...])(def v(input : e ...)))
  ((_[[v w ...]: e ...])(begin(def v(input : e ...))(input[[w ...]: e ...])))
  ((_[v : e ...])(def v(input : e ...)))((_ : op e ...)(op(input : e ...)))
  ((_(e ...))(e ...))((_ v)(def v(read)))((_ e ...)(begin(input e)...)))

(define-constant *MOD* 1000000007)
;(define-constant *MOD* 998244353)
(def-syntax add-mod! ()((_ v x)(let1 y(+ v x)(set! v(if(< y *MOD*)y(- y *MOD*))))))
(def-inline(v-add-mod! v i x)(let1 y(+(v-ref v i)x)(v-set! v i(if(< y *MOD*)y(- y *MOD*)))))
(def-syntax max! ()((_ v x)(set! v (max x v)))) (def-syntax min! ()((_ v x)(set! v (min x v))))
(def-inline(v-add! v i x)(v-set! v i(+ x(v-ref v i))))
(def-inline(ht-add! ht i x)(ht-set! ht i(+ x(ht-ref ht i 0))))
(def-inline(ht-cons! ht i x)(ht-set! ht i(cons x(ht-ref ht i '()))))
(def-syntax return () ((_ e ...) (begin (print e ...) (exit))))

;; 以下スニペットのコピペ

(define-constant MOD 1000000007)
(define-inline (expt-mod a n)
  (let loop ([k 1] [ak a] [ls '()])
    (if (<= k n) (loop (* k 2) (remainder (* ak ak) MOD) (cons (cons k ak) ls))
        (let loop2 ([ls ls] [n n] [an 1])
          (cond [(zero? n) an]
                [(> (caar ls) n) (loop2 (cdr ls) n an)]
                [else (loop2 (cdr ls) (- n (caar ls)) (remainder (* an (cdar ls)) MOD))])))))
(define-inline (inverse-mod a)
  (expt-mod a (- MOD 2)))
(define-inline (fact-mod n)
  (let loop([n n] [s 1]) (if (zero? n) s (loop (- n 1) (remainder (* s n) MOD)))))
(define-inline (nPr-mod n r)
  (let loop([n n] [r r] [s 1]) (if (zero? r) s (loop (- n 1) (- r 1) (remainder (* s n) MOD)))))
(define-inline (nCr-mod n r)
  (let ([R (min r (- n r))])
    (remainder (* (nPr-mod n R) (inverse-mod (fact-mod R))) MOD)))

;; 以下が本体
(input a n)
(print MOD)
(print (expt-mod a n))
0