結果

問題 No.319 happy b1rthday 2 me
ユーザー GrenacheGrenache
提出日時 2015-12-12 22:52:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 3,134 bytes
コンパイル時間 4,436 ms
コンパイル使用メモリ 76,576 KB
実行使用メモリ 49,796 KB
最終ジャッジ日時 2023-10-13 11:54:46
合計ジャッジ時間 6,689 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,524 KB
testcase_01 AC 46 ms
49,524 KB
testcase_02 AC 49 ms
49,360 KB
testcase_03 AC 46 ms
49,360 KB
testcase_04 AC 50 ms
49,536 KB
testcase_05 AC 48 ms
49,404 KB
testcase_06 AC 46 ms
49,208 KB
testcase_07 AC 46 ms
49,460 KB
testcase_08 AC 45 ms
49,356 KB
testcase_09 AC 45 ms
49,464 KB
testcase_10 AC 45 ms
49,360 KB
testcase_11 AC 45 ms
49,636 KB
testcase_12 AC 45 ms
49,276 KB
testcase_13 AC 45 ms
49,644 KB
testcase_14 AC 45 ms
49,796 KB
testcase_15 AC 44 ms
49,492 KB
testcase_16 AC 45 ms
49,284 KB
testcase_17 AC 45 ms
49,456 KB
testcase_18 AC 46 ms
49,444 KB
testcase_19 AC 45 ms
49,768 KB
testcase_20 AC 45 ms
49,356 KB
testcase_21 AC 43 ms
49,408 KB
testcase_22 AC 48 ms
49,388 KB
testcase_23 AC 45 ms
49,212 KB
testcase_24 AC 44 ms
47,288 KB
testcase_25 AC 45 ms
49,360 KB
testcase_26 AC 45 ms
49,340 KB
testcase_27 AC 45 ms
49,360 KB
testcase_28 AC 46 ms
49,448 KB
testcase_29 AC 45 ms
49,212 KB
testcase_30 AC 45 ms
49,300 KB
testcase_31 AC 45 ms
49,356 KB
testcase_32 AC 45 ms
49,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;


public class Main_yukicoder319 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        long a = sc.nextLong();
        long b = sc.nextLong();

        long ret = happy(b) - happy(a - 1);

        String tmp = Long.toString(a);
        if (tmp.charAt(0) == '2' && tmp.charAt(tmp.length() - 1) == '2') {
        	pr.println(ret - 1);
        } else {
        	pr.println(ret);
        }

//        long prev = 0;
//        for (long aa = 1; aa < 2000; aa++) {
//        	long tmpt = happy(aa);
//        	if (tmpt < prev) {
//        		pr.println("wrong");
//        	}
//        	pr.printf("%d : %d\n", aa, tmpt);
//        	prev = tmpt;
//        }

        pr.close();
        sc.close();
    }

	private static long happy(long a) {
		long base = 1;
		long n12 = 12;
		long ret = 0;
		while (a > base) {
			long tmp1 = a / (base * 100);
			long tmp2 = a % (base * 100);
			ret += tmp1 * base;

			long tmp3 = tmp2 - base * n12 + 1;
			tmp3 = Math.max(tmp3, 0);
			tmp3 = Math.min(tmp3, base);
			ret += tmp3;

			base *= 10;
		}

		String s = Long.toString(a);
		int n = s.length();

		if (a >= 2 && a < 22) {
			ret += 1;
		} else if (a >= 22 && a <= 111) {
				ret += 2;
		} else if (a > 111) {
			ret += 1;
			int d1 = s.charAt(n - 1) - '0';
			int d2 = s.charAt(0) - '0';

			char[] tmp = s.substring(1, n - 1).toCharArray();

			long b = 1;
			for (int i = 0; i < n - 2; i++) {
				ret += b;
				b *= 10;
			}

			if (d2 == 2) {
				long tmp1 = Long.parseLong(new String(tmp));
//					long tmp1 = 0;
//					long b2 = 1;
//					for (int i = tmp.length - 1; i >= 0; i--) {
//						tmp1 += (tmp[i] - '0' + 1) * b2;
//						b2 *= 10;
//					}

				ret += tmp1 + 1;

				if (d1 < 2) {
					ret--;
				}
			} else if (d2 > 2) {
				ret += b;
			} else {
			}
		}

		return ret;
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0