結果

問題 No.319 happy b1rthday 2 me
ユーザー りあんりあん
提出日時 2015-12-04 01:13:01
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 3,194 bytes
コンパイル時間 2,193 ms
コンパイル使用メモリ 105,460 KB
実行使用メモリ 23,940 KB
最終ジャッジ日時 2023-10-12 11:52:27
合計ジャッジ時間 5,652 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
21,992 KB
testcase_01 AC 59 ms
21,932 KB
testcase_02 AC 60 ms
21,892 KB
testcase_03 AC 60 ms
21,800 KB
testcase_04 AC 60 ms
21,920 KB
testcase_05 AC 60 ms
23,872 KB
testcase_06 AC 60 ms
21,900 KB
testcase_07 AC 60 ms
21,840 KB
testcase_08 AC 59 ms
21,832 KB
testcase_09 AC 60 ms
21,864 KB
testcase_10 AC 59 ms
19,768 KB
testcase_11 AC 60 ms
21,836 KB
testcase_12 AC 60 ms
21,756 KB
testcase_13 AC 60 ms
21,844 KB
testcase_14 AC 60 ms
23,908 KB
testcase_15 AC 59 ms
23,940 KB
testcase_16 AC 60 ms
21,832 KB
testcase_17 AC 60 ms
23,888 KB
testcase_18 AC 60 ms
19,860 KB
testcase_19 AC 61 ms
21,800 KB
testcase_20 AC 61 ms
21,940 KB
testcase_21 AC 60 ms
19,864 KB
testcase_22 AC 60 ms
23,832 KB
testcase_23 AC 60 ms
21,864 KB
testcase_24 AC 60 ms
21,816 KB
testcase_25 AC 61 ms
23,916 KB
testcase_26 AC 61 ms
21,772 KB
testcase_27 AC 60 ms
21,864 KB
testcase_28 AC 60 ms
21,824 KB
testcase_29 AC 61 ms
21,872 KB
testcase_30 AC 61 ms
21,844 KB
testcase_31 AC 60 ms
21,796 KB
testcase_32 AC 60 ms
21,812 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.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace Solver
{
    class Program
    {
        static void Main()
        {
            var s2 = new sol2();
            var a = Console.ReadLine().Split().Select(long.Parse).ToArray();
            Console.WriteLine(s2.run(a[0], a[1]));
        }
    }

    // 愚直数え上げ解法
    class sol1
    {
        public long run(long a, long b)
        {
            var sb = new StringBuilder();
            for (var i = a; i <= b; i++)
                sb.Append(i);

            var s = sb.ToString();
            long ans = 0;
            for (int i = 0; i < s.Length - 1; i++)
                if (s[i] == '1' && s[i + 1] == '2')
                    ++ans;

            return ans;
        }
    }

    // 想定解法
    class sol2
    {
        public long run(long a, long b)
        {
            var s = (a - 1).ToString();
            if (s[s.Length - 1] == '1' && a.ToString()[0] == '2')
                return count(b) - count(a - 1) - 1;

            return count(b) - count(a - 1);
        }
        long count(long a)
        {
            if (a < 2) return 0;
            if (a < 12) return 1;

            var s = a.ToString();
            int n = s.Length;
            long ret = 1;
            var l = new long[n];
            l[1] = 1;
            for (int i = 1; i < n - 1; i++)
                l[i + 1] = l[i] * 10; 
            for (int i = 0; i < n - 1; i++)
            {
                if (i == 0)
                {
                    if (s[0] == '1')
                    {
                        if (s[1] == '2')
                        {
                            if (n == 2) return 2;

                            ret += long.Parse(s.Substring(2)) + 1;
                        }
                        else if (s[1] > '2')
                            ret += l[n - 1];
                    }
                    else if (s[0] == '2')
                    {
                        if (s[n - 1] > '1')
                        {
                            if (n == 2) return 3;

                            ret += long.Parse(s.Substring(1, n - 2)) + 1;
                        }
                        else
                        {
                            if (n == 2) return 2;

                            ret += long.Parse(s.Substring(1, n - 2));
                        }
                        ret += l[n - 1];
                    }
                    else
                        ret += l[n - 1] * 2;

                    continue;
                }
                ret += (long.Parse(s.Substring(0, i)) + 1) * l[n - i - 1];
                if (s[i] == '1')
                {
                    if (s[i + 1] == '2')
                    {
                        ++ret;
                        if (i + 2 < n)
                            ret += long.Parse(s.Substring(i + 2));
                    }
                    else if (s[i + 1] > '2')
                        ret += l[n - i - 1];
                }
                else if (s[i] > '1')
                    ret += l[n - i - 1];
            }
            return ret;
        }
    }
}
0