結果

問題 No.528 10^9と10^9+7と回文
ユーザー RisenRisen
提出日時 2017-06-10 14:44:00
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,751 bytes
コンパイル時間 1,061 ms
コンパイル使用メモリ 110,844 KB
実行使用メモリ 25,284 KB
最終ジャッジ日時 2023-10-24 20:27:58
合計ジャッジ時間 2,632 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
25,184 KB
testcase_01 AC 28 ms
25,184 KB
testcase_02 AC 31 ms
25,268 KB
testcase_03 AC 31 ms
25,268 KB
testcase_04 AC 32 ms
25,280 KB
testcase_05 AC 32 ms
25,280 KB
testcase_06 AC 33 ms
25,284 KB
testcase_07 WA -
testcase_08 AC 32 ms
25,284 KB
testcase_09 WA -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 WA -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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(ulong n, int mod)
    {
        var list = n.ToString().Select(c => int.Parse(c.ToString())).ToList();
        return GetPalindromeCount(list, mod, false);
    }

    static ulong GetPalindromeCount(IEnumerable<int> n, int mod, bool allowHeadZero)
    {
        if (n.Count() == 0)
        {
            return 0;
        }
        if (n.Count() == 1)
        {
            return (ulong)(n.First() + (allowHeadZero ? 1 : 0));
        }


        ulong result = 0;
        // 全桁を使うパターン
        var t = n.ToList();
        var mult = 1UL;
        while (t.Count > 0)
        {
            var tails = t.First();
            if (t.Count > 1)
            {
                tails = t.Skip(1).Aggregate((prod, next) => prod * 10 + next);
            }
            ulong min = (ulong)Math.Min(n.First(), tails);
            if (t.Count == n.Count())
            {
                mult = min;
            }
            else
            {
                min++;  // 0をバリエーションに加える
                mult = (mult * min) % (ulong)mod;
            }
            t = t.Skip(1).Take(Math.Max(t.Count - 2, 0)).ToList();
        }
        result += mult;
        // 最上位の桁を使わないパターン
        result += GetPalindromeCount(n.Skip(1).Select(_ => 9), mod, false);
        return result;
    }

    static void Main()
    {
        var dividers = new int[] { 1000000000, 1000000007 };
        var n = ulong.Parse(Console.ReadLine());

        foreach (var div in dividers)
        {
            var pal = GetPalindromeCount(n, div);
            Console.WriteLine(pal);
        }
    }
}
0