結果

問題 No.528 10^9と10^9+7と回文
ユーザー RisenRisen
提出日時 2017-06-10 16:05:51
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,196 bytes
コンパイル時間 3,988 ms
コンパイル使用メモリ 105,964 KB
実行使用メモリ 59,172 KB
最終ジャッジ日時 2023-08-23 08:10:03
合計ジャッジ時間 24,026 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
22,680 KB
testcase_01 AC 82 ms
22,716 KB
testcase_02 AC 81 ms
22,648 KB
testcase_03 AC 82 ms
24,644 KB
testcase_04 AC 82 ms
24,680 KB
testcase_05 AC 81 ms
24,604 KB
testcase_06 AC 86 ms
22,612 KB
testcase_07 AC 84 ms
22,820 KB
testcase_08 AC 84 ms
22,444 KB
testcase_09 AC 84 ms
22,668 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 953 ms
48,284 KB
testcase_15 AC 859 ms
49,772 KB
testcase_16 AC 751 ms
43,552 KB
testcase_17 AC 643 ms
46,704 KB
testcase_18 TLE -
testcase_19 AC 461 ms
43,936 KB
testcase_20 TLE -
testcase_21 AC 639 ms
44,548 KB
testcase_22 AC 85 ms
22,640 KB
testcase_23 AC 87 ms
22,672 KB
testcase_24 AC 96 ms
22,792 KB
testcase_25 AC 190 ms
30,920 KB
testcase_26 TLE -
testcase_27 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Solution
{
    static ulong GetPalindromeCount(string n, ulong mod)
    {
        ulong result = 0;

        // 全桁を使うパターン
        var front = n.Substring(0, (n.Length + 1) / 2);
        var back = n.Substring(n.Length / 2, (n.Length + 1) / 2);

        // 100...0からhalfを数える
        var count = ulong.Parse(front.Substring(0, 1)) - 1;
        foreach (var c in front.Skip(1))
        {
            count *= 10;
            count %= mod;
            count += ulong.Parse(c.ToString());
            count %= mod;
        }
        result += count + 1;
        result %= mod;

        // reverse > back であればhalfは回文にできない.数えたものから減らす
        var reverse = new string(front.Reverse().ToArray());
        if (reverse.CompareTo(back) > 0)
        {
            result--;
        }

        // 最上位の桁を使わないパターン
        result += GetPalindromeCount0(n.Length - 1, mod);
        result %= mod;
        return result;
    }

    static Dictionary<Tuple<int, ulong>, ulong> powCache = new Dictionary<Tuple<int, ulong>, ulong>();

    // 10^nを得る
    static ulong Pow10(int n, ulong mod)
    {
        if (n == 0)
        {
            return 1;
        }

        var tuple = Tuple.Create(n, mod);
        if (powCache.ContainsKey(tuple))
        {
            return powCache[tuple];
        }

        var pow = (10 * Pow10(n - 1, mod)) % mod;
        powCache[tuple] = pow;
        return pow;
    }

    // digits桁の数について,回文のバリエーションを得る
    static ulong GetPalindromeCount0(int digits, ulong mod)
    {
        if (digits == 0)
        {
            return 0;
        }

        var pal = (9UL * Pow10((digits - 1) / 2, mod)) % mod;
        return (pal + GetPalindromeCount0(digits - 1, mod)) % mod;
    }

    static void Main()
    {
        var dividers = new ulong[] { 1000000000, 1000000007 };
        var n = Console.ReadLine();
        foreach (var div in dividers)
        {
            var pal = GetPalindromeCount(n, div);
            Console.WriteLine(pal);
        }
    }
}
0