結果

問題 No.319 happy b1rthday 2 me
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-02 22:39:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 2,915 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 74,812 KB
実行使用メモリ 56,076 KB
最終ジャッジ日時 2023-10-11 21:11:13
合計ジャッジ時間 7,671 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
56,020 KB
testcase_01 AC 118 ms
55,536 KB
testcase_02 AC 119 ms
55,788 KB
testcase_03 AC 121 ms
55,548 KB
testcase_04 AC 119 ms
55,760 KB
testcase_05 AC 119 ms
55,656 KB
testcase_06 AC 119 ms
55,772 KB
testcase_07 AC 117 ms
55,380 KB
testcase_08 AC 119 ms
55,312 KB
testcase_09 AC 119 ms
55,628 KB
testcase_10 AC 120 ms
56,076 KB
testcase_11 AC 121 ms
55,356 KB
testcase_12 AC 118 ms
55,956 KB
testcase_13 AC 120 ms
55,532 KB
testcase_14 AC 119 ms
55,708 KB
testcase_15 AC 120 ms
55,952 KB
testcase_16 AC 119 ms
55,380 KB
testcase_17 AC 123 ms
55,700 KB
testcase_18 AC 121 ms
55,852 KB
testcase_19 AC 121 ms
55,588 KB
testcase_20 AC 121 ms
56,004 KB
testcase_21 AC 127 ms
55,840 KB
testcase_22 AC 121 ms
55,568 KB
testcase_23 AC 121 ms
55,296 KB
testcase_24 AC 120 ms
55,636 KB
testcase_25 AC 119 ms
55,396 KB
testcase_26 AC 120 ms
55,956 KB
testcase_27 AC 120 ms
55,732 KB
testcase_28 AC 120 ms
55,888 KB
testcase_29 AC 121 ms
55,724 KB
testcase_30 AC 123 ms
55,640 KB
testcase_31 AC 122 ms
55,748 KB
testcase_32 AC 121 ms
55,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String A = sc.next();
    String B = sc.next();
    long ans = 0;
    long t = 0;
    for(int i = 1; i < A.length(); i++) {
      if((A.charAt(i) == '2') && (A.charAt(i - 1) == '1')) t++;
    }
    ans = (func(B) - func(A)) + t + (func2(B) - func2(A));
    System.out.println(ans); 
  }

  public static long func(String X) {
    int len = X.length();
    long[][][][] dp = new long[len][10][10][2];
    int first = Integer.parseInt(String.valueOf(X.charAt(0)));
    for(int i = 0; i < first; i++) {
      dp[0][0][i][1] = 1;
    }
    dp[0][0][first][0] = 1;
    
    for(int i = 1; i < len; i++) {
      int d = Integer.parseInt(String.valueOf(X.charAt(i)));
      for(int j = 0; j < 10; j++) {
        for(int l = 0; l <= d; l++) {
          if(l == d) {
            if(l == 2) {
              if(j < 9) dp[i][j + 1][l][0] += dp[i - 1][j][1][0];
            } else {
              dp[i][j][l][0] += dp[i - 1][j][1][0];
            }
          } else {
            if(l == 2) {
              if(j < 9) dp[i][j + 1][l][1] += dp[i - 1][j][1][0];
            } else {
              dp[i][j][l][1] += dp[i - 1][j][1][0];
            }
          }
        }
        for(int l = 0; l <= 9; l++) {
          if(l == 2) {
            if(j < 7) dp[i][j + 1][l][1] += dp[i - 1][j][1][1];
          } else {
            dp[i][j][l][1] += dp[i - 1][j][1][1];
          }
        }
        for(int k = 0; k <= 9; k++) {
        if(k != 1) {
        for(int l = 0; l <= d; l++) {
          if(l == d) {
            dp[i][j][l][0] += dp[i - 1][j][k][0];
          } else {
            dp[i][j][l][1] += dp[i - 1][j][k][0];
          }
        }
        for(int l = 0; l <= 9; l++) {
          dp[i][j][l][1] += dp[i - 1][j][k][1];
        }
        }
        }      
      }
    }

    long ret = 0;
    for(int j = 1; j < 10; j++) {
      for(int k = 0; k <= 9; k++) {
        for(int l = 0; l < 2; l++) {
          ret += (j * dp[len - 1][j][k][l]);
        }
      }
    }

    return ret;
  }

  public static long func2(String X) {
    int len = X.length();
    long ret = 0;
    for(int i = 1; i < len; i++) {
      if(i == 1) {
        ret++;
      } else {
        ret += ((long)Math.pow(10, i - 2));
      }
    }
    int first = Integer.parseInt(String.valueOf(X.charAt(0)));
    if(first == 2) {
      int last = Integer.parseInt(String.valueOf(X.charAt(len - 1)));
      if(len > 2) {
      long t = Long.parseLong(X.substring(1, len - 1));
      if(last < 2) {
        ret += t;
      } else {
        ret += (t + 1);
      }
      } else if(len == 2) {
        if(last > 1) ret++;
      }
    }
    if(first > 2) {
      if(len >= 2) ret += ((long)Math.pow(10, len - 2));
    }
    if(X.equals("1")) {
      ret = 0;
    } else {
      if(len == 1) ret = 1;
    }
    return ret;
  }
}
0