結果

問題 No.319 happy b1rthday 2 me
ユーザー りあんりあん
提出日時 2015-11-23 14:02:30
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 6,629 bytes
コンパイル時間 2,492 ms
コンパイル使用メモリ 108,292 KB
実行使用メモリ 23,936 KB
最終ジャッジ日時 2023-10-11 18:20:24
合計ジャッジ時間 6,476 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
19,648 KB
testcase_01 AC 62 ms
21,808 KB
testcase_02 AC 63 ms
21,804 KB
testcase_03 AC 61 ms
21,732 KB
testcase_04 AC 62 ms
21,900 KB
testcase_05 AC 61 ms
21,808 KB
testcase_06 AC 62 ms
21,844 KB
testcase_07 AC 62 ms
21,860 KB
testcase_08 AC 63 ms
21,824 KB
testcase_09 AC 62 ms
21,740 KB
testcase_10 AC 62 ms
21,800 KB
testcase_11 AC 63 ms
21,804 KB
testcase_12 AC 62 ms
23,884 KB
testcase_13 AC 62 ms
23,936 KB
testcase_14 AC 63 ms
21,868 KB
testcase_15 AC 61 ms
21,884 KB
testcase_16 AC 63 ms
21,824 KB
testcase_17 AC 63 ms
21,852 KB
testcase_18 AC 62 ms
21,852 KB
testcase_19 AC 61 ms
21,852 KB
testcase_20 AC 62 ms
23,720 KB
testcase_21 AC 62 ms
21,816 KB
testcase_22 AC 62 ms
21,736 KB
testcase_23 AC 62 ms
23,860 KB
testcase_24 AC 62 ms
21,816 KB
testcase_25 AC 63 ms
21,760 KB
testcase_26 AC 62 ms
23,792 KB
testcase_27 AC 63 ms
23,788 KB
testcase_28 AC 62 ms
21,936 KB
testcase_29 AC 63 ms
21,804 KB
testcase_30 AC 62 ms
21,864 KB
testcase_31 AC 62 ms
21,788 KB
testcase_32 AC 61 ms
21,824 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 sc = new Scan();
//            var s1 = new sol1();
            var s2 = new sol2();
            var a = sc.LongArr;
            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;
        }
    }
    class Scan
    {
        public int Int { get { return int.Parse(Console.ReadLine().Trim()); } }
        public long Long { get { return long.Parse(Console.ReadLine().Trim()); } }
        public string Str { get { return Console.ReadLine().Trim(); } }
        public int[] IntArr { get { return Console.ReadLine().Trim().Split().Select(int.Parse).ToArray(); } }
        public int[] IntArrWithSep(char sep) { return Console.ReadLine().Trim().Split(sep).Select(int.Parse).ToArray(); }
        public long[] LongArr { get { return Console.ReadLine().Trim().Split().Select(long.Parse).ToArray(); } }
        public double[] DoubleArr { get { return Console.ReadLine().Split().Select(double.Parse).ToArray(); } }
        public string[] StrArr { get { return Console.ReadLine().Trim().Split(); } }
        public List<int> IntList { get { return Console.ReadLine().Trim().Split().Select(int.Parse).ToList(); } }
        public List<long> LongList { get { return Console.ReadLine().Trim().Split().Select(long.Parse).ToList(); } }
        public void Multi(out int a, out int b) { var arr = IntArr; a = arr[0]; b = arr[1]; }
        public void Multi(out int a, out int b, out int c) { var arr = IntArr; a = arr[0]; b = arr[1]; c = arr[2]; }
        public void Multi(out int a, out int b, out int c, out int d) { var arr = IntArr; a = arr[0]; b = arr[1]; c = arr[2]; d = arr[3]; }
        public void Multi(out int a, out string b) { var arr = StrArr; a = int.Parse(arr[0]); b = arr[1]; }
        public void Multi(out int a, out int b, out string c) { var arr = StrArr; a = int.Parse(arr[0]); b = int.Parse(arr[1]); c = arr[2]; }
        public void Multi(out int a, out char b) { var arr = StrArr; a = int.Parse(arr[0]); b = arr[1][0]; }
        public void Multi(out char a, out int b) { var arr = StrArr; a = arr[0][0]; b = int.Parse(arr[1]); }
        public void Multi(out long a, out long b) { var arr = LongArr; a = arr[0]; b = arr[1]; }
        public void Multi(out long a, out int b) { var arr = LongArr; a = arr[0]; b = (int)arr[1]; }
        public void Multi(out string a, out string b) { var arr = StrArr; a = arr[0]; b = arr[1]; }
    }
    class mymath
    {
        int M;
        const double eps = 1e-9;
        public mymath(int M) { this.M = M; }
        public long pow(long a, long b)
        {
            if (b == 0) return 1;
            if (b == 1) return a % M;

            long t = pow(a, b / 2);

            if ((b & 1) == 0) return t * t % M;
            else return t * t % M * a % M;
        }
        public long gcd(long a, long b)
        {
            while (b != 0)
            {
                var t = a % b;
                a = b;
                b = t;
            }
            return a;
        }
        public long lcm(int a, int b) { return (a * b) / gcd(a, b); }
    }
}
0