結果

問題 No.319 happy b1rthday 2 me
ユーザー ぴろずぴろず
提出日時 2015-12-12 00:55:20
言語 Java19
(openjdk 21)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 4,392 ms
コンパイル使用メモリ 78,224 KB
実行使用メモリ 56,080 KB
最終ジャッジ日時 2023-10-13 11:32:31
合計ジャッジ時間 9,504 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
55,768 KB
testcase_01 AC 110 ms
55,836 KB
testcase_02 AC 108 ms
54,048 KB
testcase_03 AC 108 ms
55,836 KB
testcase_04 AC 110 ms
56,080 KB
testcase_05 AC 121 ms
55,372 KB
testcase_06 AC 113 ms
55,792 KB
testcase_07 AC 117 ms
55,520 KB
testcase_08 AC 113 ms
55,880 KB
testcase_09 AC 116 ms
55,640 KB
testcase_10 AC 115 ms
55,720 KB
testcase_11 AC 119 ms
56,028 KB
testcase_12 AC 114 ms
55,628 KB
testcase_13 AC 115 ms
55,948 KB
testcase_14 AC 117 ms
55,580 KB
testcase_15 AC 113 ms
56,028 KB
testcase_16 AC 116 ms
55,756 KB
testcase_17 AC 115 ms
55,884 KB
testcase_18 AC 114 ms
55,608 KB
testcase_19 AC 111 ms
55,500 KB
testcase_20 AC 113 ms
55,580 KB
testcase_21 AC 111 ms
55,896 KB
testcase_22 AC 117 ms
55,796 KB
testcase_23 AC 115 ms
53,576 KB
testcase_24 AC 114 ms
55,844 KB
testcase_25 AC 113 ms
55,268 KB
testcase_26 AC 116 ms
55,720 KB
testcase_27 AC 112 ms
55,540 KB
testcase_28 AC 115 ms
56,040 KB
testcase_29 AC 111 ms
55,508 KB
testcase_30 AC 111 ms
55,856 KB
testcase_31 AC 114 ms
55,584 KB
testcase_32 AC 114 ms
55,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no319;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		System.out.println(count1(b) - count1(a-1) + count2(b) - count2(a));
	}
	
	//0 ~ n の数で12を含むものの数
	public static long count1(long x) {
		char[] s = String.valueOf(x).toCharArray();
		int n = s.length;
		long[][][][] dp = new long[2][n][10][n+1];
		dp[1][0][0][0] = 1;
		for(int i=0;i<n;i++) {
			for(int large=0;large<2;large++) {
				for(int twelve=0;twelve<n;twelve++) {
					for(int j=0;j<=9;j++) {
						if (dp[large][twelve][j][i] == 0) {
							continue;
						}
						int max = large == 1 ? s[i] - '0' : 9;
						for(int k=0;k<=max;k++) {
							int nlarge = (large == 1 && k == s[i] - '0') ? 1 : 0;
							int ntwelve = (j == 1 && k == 2) ? twelve + 1 : twelve;
							dp[nlarge][ntwelve][k][i+1] += dp[large][twelve][j][i];
						}
					}
				}

			}
		}
		long ans = 0;
		for(int i=0;i<2;i++) {
			for(int j=1;j<n;j++) {
				for(int k=0;k<10;k++) {
					ans += dp[i][j][k][n] * j;
				}
			}
		}
//		System.out.println("c1:" + x + "," + ans);
		return ans;
	}
	
	//0 ~ x の中で2で始まって2で終わるものの数
	public static long count2(long x) {
		long ans = 0;
		if (x >= 2) {
			ans++;
		}
		if (x >= 22) {
			ans++;
		}
		long y = 202;
		long powten = 10;
		while(x >= y) {
			ans += Math.min((x - y) / 10,powten - 1) + 1;
			y = y * 10 - 18;
			powten *= 10;
		}
//		System.out.println("c2:" + x + "," + ans);
		return ans;
	}

}
0