結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
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));
        }

        var tails = n.Skip(1).Aggregate((prod, next) => prod * 10 + next);
        var mult = Math.Min(n.First(), tails);

        var mid = tails / 10;
        ulong result = 0;
        // 全桁を使うパターン
        result += GetPalindromeCount0(Math.Min(n.Count() - 1, 0), mod) * (ulong)mult;
        // 最上位の桁を使わないパターン
        result += GetPalindromeCount(n.Skip(1).Select(_ => 9), mod, false);
        return result;
    }

    // digits桁の数について,回文のバリエーションを得る
    static ulong GetPalindromeCount0(int digits, int mod)
    {
        return Enumerable.Repeat(10, (digits +1)/2).Aggregate(1UL, (prod, next) => (prod * (ulong)next) % (ulong)mod);
    }

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

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

    }
}
0