結果

問題 No.528 10^9と10^9+7と回文
ユーザー RisenRisen
提出日時 2017-06-10 15:36:02
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,716 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 110,740 KB
実行使用メモリ 25,868 KB
最終ジャッジ日時 2023-10-24 20:29:09
合計ジャッジ時間 3,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
25,868 KB
testcase_01 AC 29 ms
25,824 KB
testcase_02 AC 31 ms
25,856 KB
testcase_03 AC 31 ms
25,852 KB
testcase_04 AC 30 ms
25,856 KB
testcase_05 AC 31 ms
25,856 KB
testcase_06 WA -
testcase_07 AC 30 ms
25,856 KB
testcase_08 WA -
testcase_09 AC 31 ms
25,860 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Linq;

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

        // 全桁を使うパターン
        var half = new string(n.Take((n.Length + 1) / 2).ToArray());
        var reverse = new string(n.Reverse().Take((n.Length + 1) / 2).ToArray());

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

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

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

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

        var pal = (9UL * Enumerable.Repeat(10, (digits - 1) / 2).Aggregate(1UL, (prod, next) => (prod * (ulong)next) % 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