結果
| 問題 |
No.528 10^9と10^9+7と回文
|
| ユーザー |
Risen
|
| 提出日時 | 2017-06-10 14:24:44 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,671 bytes |
| コンパイル時間 | 976 ms |
| コンパイル使用メモリ | 112,196 KB |
| 実行使用メモリ | 29,296 KB |
| 最終ジャッジ日時 | 2024-09-24 15:34:54 |
| 合計ジャッジ時間 | 2,394 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 11 RE * 17 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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);
}
}
}
Risen