結果

問題 No.528 10^9と10^9+7と回文
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-06-18 14:27:17
言語 Python2
(2.7.18)
結果
AC  
実行時間 139 ms / 1,000 ms
コード長 1,155 bytes
コンパイル時間 982 ms
コンパイル使用メモリ 6,752 KB
実行使用メモリ 10,456 KB
最終ジャッジ日時 2023-10-01 00:59:56
合計ジャッジ時間 2,767 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,864 KB
testcase_01 AC 11 ms
5,860 KB
testcase_02 AC 12 ms
5,972 KB
testcase_03 AC 12 ms
5,864 KB
testcase_04 AC 12 ms
5,888 KB
testcase_05 AC 11 ms
5,884 KB
testcase_06 AC 11 ms
6,008 KB
testcase_07 AC 11 ms
5,940 KB
testcase_08 AC 11 ms
5,948 KB
testcase_09 AC 11 ms
5,884 KB
testcase_10 AC 129 ms
8,256 KB
testcase_11 AC 130 ms
8,252 KB
testcase_12 AC 135 ms
10,456 KB
testcase_13 AC 137 ms
10,372 KB
testcase_14 AC 76 ms
8,544 KB
testcase_15 AC 67 ms
8,260 KB
testcase_16 AC 61 ms
7,984 KB
testcase_17 AC 53 ms
7,600 KB
testcase_18 AC 126 ms
10,360 KB
testcase_19 AC 36 ms
6,560 KB
testcase_20 AC 86 ms
7,716 KB
testcase_21 AC 52 ms
7,508 KB
testcase_22 AC 11 ms
5,940 KB
testcase_23 AC 11 ms
6,116 KB
testcase_24 AC 12 ms
6,068 KB
testcase_25 AC 20 ms
6,276 KB
testcase_26 AC 130 ms
8,236 KB
testcase_27 AC 139 ms
8,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python2
# -*- coding: utf-8 -*-
# †
mod0 = 10**9
mod7 = 10**9 + 7

def minus1(a):
    n = len(a)
    for i in reversed(range(n)):
        if a[i] == 0:
            a[i] = 9
        else:
            a[i] -= 1
            break
    return a


# a以下の最大の回文数
# aの例: [1, 4, 1, 4, 2, 1, 3, 5, 6]
def largest_palnum(a):
    n = len(a)
    # 10, 100, 1000, 10000, ... は例外
    if n > 1 and a[0] == 1 and all(a[i]==0 for i in xrange(1, n)):
        return [9] * (n-1)
    m = (n + 1) / 2
    def _create(vl):
        vr = list(reversed(vl))
        if n & 1:
            vr.pop(0)
        return vl + vr
    r1 = _create(a[:m])
    if a >= r1:
        return r1
    r2 = _create(minus1(a[:m]))
    return r2


# s以下の回文数の個数
# s: 整数を文字列で
def count(s, mod):
    a = largest_palnum(map(int, s))
    N = len(a)
    res =  pow(10, N/2,     mod) - 1
    res += pow(10, (N-1)/2, mod) - 1
    M = (N + 1) / 2
    x = int(''.join(map(str, a[:M])))
    res += x - pow(10, M-1, mod) + 1
    res %= mod
    return res

###
s = raw_input()
res0 = count(s, mod0)
res7 = count(s, mod7)
print res0
print res7
0