結果
| 問題 |
No.260 世界のなんとか3
|
| コンテスト | |
| ユーザー |
eitaho
|
| 提出日時 | 2015-08-07 20:25:25 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 665 ms / 2,000 ms |
| コード長 | 3,671 bytes |
| コンパイル時間 | 1,849 ms |
| コンパイル使用メモリ | 110,720 KB |
| 実行使用メモリ | 29,568 KB |
| 最終ジャッジ日時 | 2024-11-07 18:08:43 |
| 合計ジャッジ時間 | 10,647 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;
class Program
{
static readonly int Mod = (int)1e9 + 7;
void Add(ref int a, int b) { if ((a += b) >= Mod) a -= Mod; }
public void Solve()
{
var A = Reader.String();
var B = Reader.String();
int ans = ((Count(B) - Count((BigInteger.Parse(A) - 1) + "")) % Mod + Mod) % Mod;
Console.WriteLine(ans);
}
private int Count(string S)
{
int N = S.Length;
var dp = new int[N + 1, 2, 3, 2, 8]; // index, smaller, mod3, contains3, mod8
dp[0, 0, 0, 0, 0] = 1;
for (int i = 0; i < N; i++)
for (int smaller = 0; smaller < 2; smaller++)
for (int d = 0; d <= 9; d++)
if (smaller == 1 || d <= S[i] - '0')
for (int mod3 = 0; mod3 < 3; mod3++)
for (int has3 = 0; has3 < 2; has3++)
for (int mod8 = 0; mod8 < 8; mod8++)
{
int ns = smaller | (d < S[i] - '0' ? 1 : 0);
int nmod3 = (mod3 + d) % 3;
int nhas3 = has3 | (d == 3 ? 1 : 0);
int nmod8 = (mod8 * 10 + d) % 8;
Add(ref dp[i + 1, ns, nmod3, nhas3, nmod8], dp[i, smaller, mod3, has3, mod8]);
}
int res = 0;
for (int smaller = 0; smaller < 2; smaller++)
for (int mod8 = 1; mod8 < 8; mod8++)
{
Add(ref res, dp[N, smaller, 0, 0, mod8]);
for (int mod3 = 0; mod3 < 3; mod3++)
Add(ref res, dp[N, smaller, mod3, 1, mod8]);
}
return res;
}
}
class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
private static TextReader reader = Console.In;
private static readonly char[] separator = { ' ' };
private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
private static string[] A = new string[0];
private static int i;
private static void Init() { A = new string[0]; }
public static void Set(TextReader r) { reader = r; Init(); }
public static void Set(string file) { reader = new StreamReader(file); Init(); }
public static bool HasNext() { return CheckNext(); }
public static string String() { return Next(); }
public static int Int() { return int.Parse(Next()); }
public static long Long() { return long.Parse(Next()); }
public static double Double() { return double.Parse(Next()); }
public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
public static string Line() { return reader.ReadLine().Trim(); }
private static string[] Split(string s) { return s.Split(separator, op); }
private static string Next() { CheckNext(); return A[i++]; }
private static bool CheckNext()
{
if (i < A.Length) return true;
string line = reader.ReadLine();
if (line == null) return false;
if (line == "") return CheckNext();
A = Split(line);
i = 0;
return true;
}
}
eitaho