結果
問題 |
No.528 10^9と10^9+7と回文
|
ユーザー |
![]() |
提出日時 | 2019-06-22 15:27:28 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 402 ms / 1,000 ms |
コード長 | 2,269 bytes |
コンパイル時間 | 960 ms |
コンパイル使用メモリ | 115,124 KB |
実行使用メモリ | 54,684 KB |
最終ジャッジ日時 | 2024-07-23 18:35:54 |
合計ジャッジ時間 | 6,541 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
コンパイルメッセージ
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; public class Solution { static ulong GetPalindromeCount(string n, ulong mod) { ulong result = 0; // 全桁を使うパターン var front = n.Substring(0, (n.Length + 1) / 2); var back = n.Substring(n.Length / 2, (n.Length + 1) / 2); // 100...0からhalfを数える var count = ulong.Parse(front.Substring(0, 1)) - 1; foreach (var c in front.Skip(1)) { count *= 10; count %= mod; count += ulong.Parse(c.ToString()); count %= mod; } result += count + 1; result %= mod; // reverse > back であればhalfは回文にできない.数えたものから減らす var reverse = new string(front.Reverse().ToArray()); if (reverse.CompareTo(back) > 0) { result--; } // 最上位の桁を使わないパターン result += GetPalindromeCount0(n.Length - 1, mod); result %= mod; return result; } static Dictionary<Tuple<int, ulong>, ulong> powCache = new Dictionary<Tuple<int, ulong>, ulong>(); // 10^nを得る static ulong Pow10(int n, ulong mod) { var tuple = Tuple.Create(n, mod); return powCache[tuple]; } static void InitPow10(int n, ulong mod) { ulong pow = 1; for (int i = 0; i <= n; i++) { var tuple = Tuple.Create(i, mod); powCache.Add(tuple, pow); pow = pow * 10 % mod; } } // digits桁の数について,回文のバリエーションを得る static ulong GetPalindromeCount0(int digits, ulong mod) { InitPow10(digits, mod); ulong pattern = 0; for (; digits > 0; digits--) { pattern += 9UL * Pow10((digits - 1) / 2, mod); pattern %= mod; } return pattern; } public 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); } } }