結果

問題 No.2989 Fibonacci Prize
ユーザー 👑 rin204
提出日時 2024-12-14 12:54:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 1,245 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 82,144 KB
実行使用メモリ 209,148 KB
最終ジャッジ日時 2024-12-14 12:55:06
合計ジャッジ時間 8,081 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 77
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve(n, m):
    if n == 1 and m == 1:
        return 2
    if m <= 10:
        F = [1, 1]
        while len(F) < m:
            F.append(F[-1] + F[-2])

        ans = 0
        for l in range(m):
            tot = 0
            for r in range(l, m):
                tot += F[r]
                if tot % n == 0 and tot <= F[m - 1]:
                    ans += 1

        return ans

    if n == 1:
        ans = m * (m + 1) // 2
        ans -= m - 1
        if m >= 3:
            ans -= m - 3
        return ans

    a = 0
    b = 1
    cnt = [0] * n
    tot = 0
    lst = []
    F = []
    while 1:
        tot = (tot + a) % n
        lst.append(tot)
        F.append(a)
        cnt[tot] += 1

        a, b = b, (a + b) % n
        if a == 0 and b == 1:
            break

    C = [0] * n
    le = len(lst)
    loop = (m - 1) // le
    for i in range(n):
        C[i] = cnt[i] * loop

    for i in range((m - 1) % le):
        C[lst[i]] += 1

    ans = 0
    for c in C:
        ans += c * (c - 1) // 2

    a = F[m % le - 2]
    b = F[m % le - 1]
    c = F[m % le]

    if (a + b) % n == 0:
        ans += 1
    if b % n == 0:
        ans += 1
    if c % n == 0:
        ans += 1

    return ans


print(solve(*map(int, input().split())))
0