結果

問題 No.528 10^9と10^9+7と回文
ユーザー Risen
提出日時 2017-06-10 12:07:17
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,619 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 113,156 KB
実行使用メモリ 27,476 KB
最終ジャッジ日時 2024-09-24 15:30:28
合計ジャッジ時間 3,394 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 WA * 8 RE * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
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
{
    // nについて,すべての桁が1以上である場合の回文のバリエーションを得る
    static ulong GetPalindromeCount1(ulong n, int mod)
    {
        var list = n.ToString().Select(c => int.Parse(c.ToString())).ToList();
        var reverse = ((IEnumerable<int>)list).Reverse();

        // 最上位は0を取れないのでバリエーションを1減らす
        if (n > 0)
        {
            list[0]--;
        }

        // 半分で折り返して重ねて,各桁の最小値+1の積を取る
        // +1は0がバリエーションにあるため
        var count = list.Take((list.Count + 1) / 2).Zip(reverse, (i, j) => Math.Min(i, j) + 1).Aggregate(1UL, (prod, next) => (prod * (ulong)next) % (ulong)mod);
        return count;
    }

    // nについて,最上位桁が0である場合の回文のバリエーションを得る
    static ulong GetPalindromeCount0(ulong n, int mod)
    {
        // 桁数
        var digits = n.ToString().Length - 1;
        if (digits <= 0)
        {
            return 0;
        }
        var nines = ulong.Parse(new string(Enumerable.Repeat('9', digits).ToArray()));
        return GetPalindromeCount1(nines, mod);
    }


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

        foreach (var div in dividers)
        {
            var pal = GetPalindromeCount0(n, div) + GetPalindromeCount1(n, div);
            Console.WriteLine(pal);
        }

    }
}
0