結果

問題 No.319 happy b1rthday 2 me
ユーザー りあんりあん
提出日時 2015-11-23 14:53:54
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 3,787 bytes
コンパイル時間 2,260 ms
コンパイル使用メモリ 107,536 KB
実行使用メモリ 24,040 KB
最終ジャッジ日時 2023-10-11 18:21:56
合計ジャッジ時間 5,636 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
24,040 KB
testcase_01 AC 61 ms
21,840 KB
testcase_02 AC 61 ms
21,964 KB
testcase_03 AC 61 ms
22,008 KB
testcase_04 AC 60 ms
21,888 KB
testcase_05 AC 59 ms
21,992 KB
testcase_06 AC 60 ms
23,940 KB
testcase_07 AC 60 ms
23,924 KB
testcase_08 AC 60 ms
21,824 KB
testcase_09 AC 59 ms
21,932 KB
testcase_10 AC 59 ms
21,912 KB
testcase_11 AC 60 ms
23,824 KB
testcase_12 AC 61 ms
21,892 KB
testcase_13 AC 61 ms
19,800 KB
testcase_14 AC 60 ms
21,852 KB
testcase_15 AC 61 ms
21,960 KB
testcase_16 AC 60 ms
21,980 KB
testcase_17 AC 60 ms
21,916 KB
testcase_18 AC 62 ms
21,964 KB
testcase_19 AC 60 ms
21,740 KB
testcase_20 AC 60 ms
21,848 KB
testcase_21 AC 61 ms
23,820 KB
testcase_22 AC 61 ms
21,916 KB
testcase_23 AC 61 ms
23,880 KB
testcase_24 AC 61 ms
21,788 KB
testcase_25 AC 61 ms
21,912 KB
testcase_26 AC 60 ms
21,856 KB
testcase_27 AC 60 ms
21,940 KB
testcase_28 AC 59 ms
19,744 KB
testcase_29 AC 60 ms
21,892 KB
testcase_30 AC 60 ms
21,872 KB
testcase_31 AC 61 ms
21,928 KB
testcase_32 AC 60 ms
22,000 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
    {
        const int M = 1000000007;
        const double eps = 1e-9;
        static void Main()
        {
            var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            var s2 = new sol2();
            var a = Console.ReadLine().Split().Select(long.Parse).ToArray();
            sw.WriteLine(s2.run(a[0], a[1]));
            sw.Flush();
        }
    }
    class sol1
    {
        public int run(int a, int b)
        {
            var sb = new StringBuilder();
            for (int i = a; i <= b; i++)
                sb.Append(i);

            var s = sb.ToString();
            int 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 += l[n - i - 1];
                if (s[i] == '1')
                {
                    if (s[i + 1] == '2')
                    {
                        if (i + 2 == n)
                            ret += long.Parse(s.Substring(0, i)) * l[n - i - 1] + 1;
                        else
                            ret += long.Parse(s.Substring(0, i)) * l[n - i - 1] + long.Parse(s.Substring(i + 2)) + 1;
                    }
                    else if (s[i + 1] > '2')
                        ret += (long.Parse(s.Substring(0, i)) + 1) * l[n - i - 1];
                    else
                        ret += long.Parse(s.Substring(0, i)) * l[n - i - 1];
                }
                else if (s[i] > '1')
                    ret += (long.Parse(s.Substring(0, i)) + 1) * l[n - i - 1];
                else
                    ret += long.Parse(s.Substring(0, i)) * l[n - i - 1];
            }
            return ret;
        }
    }
}
0