結果

問題 No.554 recurrence formula
ユーザー maimai
提出日時 2017-04-14 20:37:17
言語 Ruby
(3.3.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 731 bytes
コンパイル時間 47 ms
コンパイル使用メモリ 11,448 KB
実行使用メモリ 15,344 KB
最終ジャッジ日時 2023-09-25 19:22:09
合計ジャッジ時間 2,947 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,256 KB
testcase_01 AC 80 ms
15,316 KB
testcase_02 AC 81 ms
15,052 KB
testcase_03 AC 79 ms
15,316 KB
testcase_04 AC 79 ms
15,340 KB
testcase_05 AC 80 ms
15,164 KB
testcase_06 AC 82 ms
15,200 KB
testcase_07 AC 81 ms
15,188 KB
testcase_08 AC 80 ms
15,044 KB
testcase_09 AC 83 ms
15,244 KB
testcase_10 AC 82 ms
15,304 KB
testcase_11 AC 84 ms
15,252 KB
testcase_12 AC 81 ms
15,056 KB
testcase_13 AC 82 ms
15,096 KB
testcase_14 AC 85 ms
15,080 KB
testcase_15 AC 83 ms
15,196 KB
testcase_16 AC 81 ms
15,124 KB
testcase_17 AC 80 ms
15,252 KB
testcase_18 AC 80 ms
15,124 KB
testcase_19 AC 91 ms
15,060 KB
testcase_20 AC 93 ms
15,344 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

@mod = (1e9+7).to_i

#
# 数列aを数列x,数列yに分割.
# a_1 , a_2 , a_3 , a_4 , a_5 , a_6 ...
# x_1 , y_1 , x_2 , y_2 , x_3 , y_3 ...
# 
# 整理するとこんな感じ
# x_p = (y_1 + y_2 + ... + y_{p-1}) (2p-1)
# y_q = (x_1 + x_2 + ... + x_{q-1}) (2q)
#
# あとは,x_1=1,y_1=2で初期化し,4変数によるdpで回す.
# 計算量はO(N)

def solve(n)
    x = 1
    y = 2
    xs = x
    ys = y
    
    3.upto(n){|i|
        if i.odd?
            x  = (ys*i) %@mod
            xs = (xs + x) %@mod
        else
            y  = (xs*i) %@mod
            ys = (ys + y) %@mod
        end
    }
    return n.odd? ? x : y
end

n = gets.chomp!.to_i
abort unless !gets
abort unless 1 <= n && n <= 100000

p solve(n)
0