結果

問題 No.1269 I hate Fibonacci Number
ユーザー 👑 rin204rin204
提出日時 2023-12-29 16:22:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,650 ms / 3,000 ms
コード長 2,289 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 77,724 KB
最終ジャッジ日時 2023-12-29 16:22:50
合計ジャッジ時間 15,965 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,608 KB
testcase_01 AC 39 ms
55,608 KB
testcase_02 AC 109 ms
76,188 KB
testcase_03 AC 39 ms
55,608 KB
testcase_04 AC 40 ms
55,608 KB
testcase_05 AC 40 ms
55,608 KB
testcase_06 AC 40 ms
55,608 KB
testcase_07 AC 43 ms
55,608 KB
testcase_08 AC 40 ms
55,608 KB
testcase_09 AC 39 ms
55,608 KB
testcase_10 AC 43 ms
55,608 KB
testcase_11 AC 41 ms
55,608 KB
testcase_12 AC 40 ms
55,608 KB
testcase_13 AC 145 ms
76,348 KB
testcase_14 AC 1,402 ms
77,084 KB
testcase_15 AC 233 ms
76,312 KB
testcase_16 AC 1,590 ms
77,724 KB
testcase_17 AC 92 ms
76,048 KB
testcase_18 AC 1,650 ms
77,604 KB
testcase_19 AC 1,101 ms
76,700 KB
testcase_20 AC 304 ms
76,316 KB
testcase_21 AC 884 ms
76,828 KB
testcase_22 AC 957 ms
76,704 KB
testcase_23 AC 911 ms
76,576 KB
testcase_24 AC 454 ms
76,704 KB
testcase_25 AC 558 ms
76,440 KB
testcase_26 AC 109 ms
76,192 KB
testcase_27 AC 51 ms
66,512 KB
testcase_28 AC 114 ms
76,332 KB
testcase_29 AC 534 ms
76,428 KB
testcase_30 AC 213 ms
76,300 KB
testcase_31 AC 1,132 ms
77,600 KB
testcase_32 AC 591 ms
76,572 KB
testcase_33 AC 232 ms
76,728 KB
testcase_34 AC 53 ms
68,568 KB
testcase_35 AC 195 ms
76,156 KB
testcase_36 AC 42 ms
55,608 KB
testcase_37 AC 39 ms
55,608 KB
testcase_38 AC 41 ms
55,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


class AhoCorasick:
    def __init__(self, words=None):
        if words is None:
            self.words = []
        else:
            self.words = words

    def register(self, word):
        self.words.append(word)

    def build(self):
        self.children = [{}]
        self.match = [[]]

        for i, word in enumerate(self.words):
            self._register(i, word)

        self.failure = [0] * len(self.children)
        self._create_failure()

    def _register(self, i, word):
        k = 0
        for s in word:
            if s in self.children[k]:
                k = self.children[k][s]
            else:
                le = len(self.children)
                self.children[k][s] = le
                self.children.append({})
                self.match.append([])
                k = le

        self.match[k].append(i)

    def _create_failure(self):
        queue = deque(self.children[0].values())
        while queue:
            k = queue.popleft()
            b = self.failure[k]
            for s, j in self.children[k].items():
                self.failure[j] = self._next(b, s)
                self.match[j] += self.match[self.failure[j]]
                queue.append(j)

    def _next(self, k, s):
        while 1:
            if s in self.children[k]:
                return self.children[k][s]
            elif k == 0:
                return 0

            k = self.failure[k]

    def search(self, text):
        k = 0
        le = len(text)
        matched = [[] for _ in range(le)]
        for i, s in enumerate(text):
            k = self._next(k, s)
            for m in self.match[k]:
                matched[i - len(self.words[m]) + 1].append(m)

        return matched


MOD = 10**9 + 7
n, l, r = map(int, input().split())
aho = AhoCorasick()
a = 1
b = 1
while b <= r:
    if l <= b <= r:
        aho.register(str(b))
    a, b = b, a + b

aho.build()

m = len(aho.children)
dp = [0] * m
dp[0] = 1
for _ in range(n):
    ndp = [0] * m
    for k, v in enumerate(dp):
        if v == 0:
            continue
        for i in range(10):
            nex = aho._next(k, str(i))
            if aho.match[nex]:
                continue

            ndp[nex] += v

    dp = [d % MOD for d in ndp]

print((sum(dp) - 1) % MOD)
0