結果

問題 No.528 10^9と10^9+7と回文
ユーザー nebukuro09nebukuro09
提出日時 2017-06-10 00:24:04
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 56 ms / 1,000 ms
コード長 1,091 bytes
コンパイル時間 957 ms
コンパイル使用メモリ 94,020 KB
実行使用メモリ 6,920 KB
最終ジャッジ日時 2023-09-03 14:08:42
合計ジャッジ時間 2,451 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 56 ms
5,320 KB
testcase_11 AC 55 ms
6,688 KB
testcase_12 AC 55 ms
5,272 KB
testcase_13 AC 55 ms
6,360 KB
testcase_14 AC 33 ms
5,480 KB
testcase_15 AC 30 ms
5,824 KB
testcase_16 AC 28 ms
6,920 KB
testcase_17 AC 23 ms
4,544 KB
testcase_18 AC 53 ms
6,352 KB
testcase_19 AC 16 ms
4,380 KB
testcase_20 AC 38 ms
6,024 KB
testcase_21 AC 22 ms
4,624 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 7 ms
4,380 KB
testcase_26 AC 54 ms
5,820 KB
testcase_27 AC 45 ms
6,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

long solve(string N, long M) {
    long ans = 0;
    auto K = N.length.to!int;

    auto dp = new long[][](K/2+K%2+1, 2);
    dp[0][0] = 1;

    foreach (i; 0..K/2+K%2) {
        foreach (j; 0..2) {
            int d = j ? 9 : N[i] - '0';
            foreach (k; 0..d+1) {
                if (i == 0 && k == 0) continue;
                (dp[i+1][j || (k < N[i] - '0')] += dp[i][j]) %= M;
            }
        }
    }

    foreach (j; 0..2) {
        (ans += dp[K/2+K%2][j]) %= M;
    }

    string NN = "";
    foreach (i; 0..N.length) if (i < K / 2 + K % 2) NN ~= N[i]; else NN ~= N[$-i-1];
    if (NN > N) ans = (ans - 1 + M) % M;

    long tmp = 9;
    foreach (k; 1..K) {
        if (k >= 3 && k % 2 == 1) tmp = tmp * 10 % M;
        ans = (ans + tmp) % M;
    }


    return ans;
}


void main() {

    auto N = readln.chomp;
    solve(N, 10^^9).writeln;
    solve(N, 10^^9+7).writeln;
}
0