結果

問題 No.528 10^9と10^9+7と回文
ユーザー RisenRisen
提出日時 2019-06-22 15:27:28
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 755 ms / 1,000 ms
コード長 2,269 bytes
コンパイル時間 3,735 ms
コンパイル使用メモリ 107,772 KB
実行使用メモリ 51,376 KB
最終ジャッジ日時 2023-10-01 01:00:53
合計ジャッジ時間 13,061 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
22,744 KB
testcase_01 AC 87 ms
22,596 KB
testcase_02 AC 88 ms
22,544 KB
testcase_03 AC 86 ms
22,692 KB
testcase_04 AC 88 ms
20,764 KB
testcase_05 AC 86 ms
20,716 KB
testcase_06 AC 87 ms
22,600 KB
testcase_07 AC 87 ms
22,628 KB
testcase_08 AC 87 ms
22,588 KB
testcase_09 AC 87 ms
22,616 KB
testcase_10 AC 741 ms
49,304 KB
testcase_11 AC 748 ms
49,288 KB
testcase_12 AC 746 ms
49,776 KB
testcase_13 AC 749 ms
49,808 KB
testcase_14 AC 476 ms
39,424 KB
testcase_15 AC 431 ms
35,952 KB
testcase_16 AC 410 ms
37,624 KB
testcase_17 AC 351 ms
36,948 KB
testcase_18 AC 708 ms
51,376 KB
testcase_19 AC 262 ms
34,068 KB
testcase_20 AC 533 ms
41,860 KB
testcase_21 AC 350 ms
38,972 KB
testcase_22 AC 88 ms
22,752 KB
testcase_23 AC 91 ms
24,740 KB
testcase_24 AC 96 ms
24,880 KB
testcase_25 AC 155 ms
33,212 KB
testcase_26 AC 745 ms
49,744 KB
testcase_27 AC 755 ms
49,296 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

public 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)
    {
        var tuple = Tuple.Create(n, mod);
        return powCache[tuple];
    }

    static void InitPow10(int n, ulong mod)
    {
        ulong pow = 1;
        for (int i = 0; i <= n; i++)
        {
            var tuple = Tuple.Create(i, mod);
            powCache.Add(tuple, pow);
            pow = pow * 10 % mod;
        }
    }

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

            pattern += 9UL * Pow10((digits - 1) / 2, mod);
            pattern %= mod;
        }

        return pattern;

    }

    public 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