結果

問題 No.260 世界のなんとか3
ユーザー eitahoeitaho
提出日時 2015-08-07 20:25:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 670 ms / 2,000 ms
コード長 3,671 bytes
コンパイル時間 1,007 ms
コンパイル使用メモリ 109,696 KB
実行使用メモリ 29,568 KB
最終ジャッジ日時 2024-04-25 07:48:35
合計ジャッジ時間 9,851 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
18,432 KB
testcase_01 AC 32 ms
18,304 KB
testcase_02 AC 32 ms
18,560 KB
testcase_03 AC 557 ms
29,568 KB
testcase_04 AC 554 ms
29,440 KB
testcase_05 AC 121 ms
19,072 KB
testcase_06 AC 91 ms
18,688 KB
testcase_07 AC 367 ms
27,008 KB
testcase_08 AC 252 ms
21,760 KB
testcase_09 AC 158 ms
20,352 KB
testcase_10 AC 425 ms
27,648 KB
testcase_11 AC 391 ms
27,264 KB
testcase_12 AC 237 ms
23,424 KB
testcase_13 AC 93 ms
19,584 KB
testcase_14 AC 351 ms
26,624 KB
testcase_15 AC 115 ms
21,248 KB
testcase_16 AC 321 ms
25,984 KB
testcase_17 AC 251 ms
25,216 KB
testcase_18 AC 247 ms
25,088 KB
testcase_19 AC 302 ms
26,112 KB
testcase_20 AC 225 ms
24,704 KB
testcase_21 AC 198 ms
21,632 KB
testcase_22 AC 357 ms
26,752 KB
testcase_23 AC 62 ms
18,948 KB
testcase_24 AC 270 ms
25,216 KB
testcase_25 AC 347 ms
21,504 KB
testcase_26 AC 205 ms
21,504 KB
testcase_27 AC 32 ms
18,304 KB
testcase_28 AC 555 ms
29,568 KB
testcase_29 AC 670 ms
29,568 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.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;
    }
}
0