AtCoder Beginner Contest 007

Submission #1771773

Source codeソースコード

import java.util.Arrays;

public class Main {

  static int[] a = new int[20];
  static int[] b = new int[20];
  private static void solve() {
    long A = nl();
    long B = nl();
    long total = B - A + 1;
    for (int i = 0; i < 20; i ++) {
      a[i] = (int)(A % 10);
      b[i] = (int)(B % 10);
      A /= 10;
      B /= 10;
    }
    
    for (long[][] v : memo) for (long[] u : v) {
      Arrays.fill(u, -1);
    }
    System.out.println(total - dfs(19, 1, 1));
  }
  
  
  static long[][][] memo = new long[20][2][2];
  private static long dfs(int i, int minFlg, int maxFlg) {
    if (i < 0) return 1;
    if (memo[i][minFlg][maxFlg] >= 0) return memo[i][minFlg][maxFlg];

    long ret = 0;
    for (int d = 0; d < 10; d ++) {
      if (d == 4 || d == 9) continue;
      if ((minFlg == 0 || a[i] <= d) && (maxFlg == 0 || d <= b[i])) {
        ret += dfs(i - 1, minFlg == 1 && a[i] == d ? 1 : 0, maxFlg == 1 && b[i] == d ? 1 : 0);
      }
    }
    memo[i][minFlg][maxFlg] = ret;
    return ret;
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = System.getProperty("debug");
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}

Submission

Task問題 D - 禁止された数字
User nameユーザ名 あやせひろみ
Created time投稿日時
Language言語 Java8 (OpenJDK 1.8.0)
Status状態 AC
Score得点 100
Source lengthソースコード長 3477 Byte
File nameファイル名
Exec time実行時間 75 ms
Memory usageメモリ使用量 23380 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask0_sample04.txt
Subtask1 30 / 30 subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask1_13.txt,subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt
Subtask2 70 / 70 subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask0_sample04.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask1_13.txt,subtask2_01.txt,subtask2_02.txt,subtask2_03.txt,subtask2_04.txt,subtask2_05.txt,subtask2_06.txt,subtask2_07.txt,subtask2_08.txt,subtask2_09.txt,subtask2_10.txt,subtask2_11.txt,subtask2_12.txt,subtask2_13.txt,subtask2_14.txt,subtask2_15.txt,subtask2_16.txt,subtask2_17.txt,subtask2_18.txt,subtask2_19.txt,subtask2_20.txt,subtask2_21.txt,subtask2_22.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_sample01.txt AC 70 ms 22612 KB
subtask0_sample02.txt AC 70 ms 21204 KB
subtask0_sample03.txt AC 70 ms 21204 KB
subtask0_sample04.txt AC 71 ms 17620 KB
subtask1_01.txt AC 73 ms 20948 KB
subtask1_02.txt AC 69 ms 19412 KB
subtask1_03.txt AC 71 ms 17108 KB
subtask1_04.txt AC 71 ms 20692 KB
subtask1_05.txt AC 75 ms 19156 KB
subtask1_06.txt AC 72 ms 21332 KB
subtask1_07.txt AC 71 ms 18516 KB
subtask1_08.txt AC 71 ms 21332 KB
subtask1_09.txt AC 71 ms 19728 KB
subtask1_10.txt AC 71 ms 19284 KB
subtask1_11.txt AC 73 ms 20692 KB
subtask1_12.txt AC 73 ms 21716 KB
subtask1_13.txt AC 72 ms 21204 KB
subtask2_01.txt AC 73 ms 20368 KB
subtask2_02.txt AC 71 ms 19156 KB
subtask2_03.txt AC 73 ms 19156 KB
subtask2_04.txt AC 72 ms 18900 KB
subtask2_05.txt AC 71 ms 19284 KB
subtask2_06.txt AC 70 ms 21392 KB
subtask2_07.txt AC 72 ms 21204 KB
subtask2_08.txt AC 73 ms 20820 KB
subtask2_09.txt AC 71 ms 21204 KB
subtask2_10.txt AC 70 ms 17620 KB
subtask2_11.txt AC 72 ms 21204 KB
subtask2_12.txt AC 72 ms 21332 KB
subtask2_13.txt AC 70 ms 21588 KB
subtask2_14.txt AC 71 ms 23380 KB
subtask2_15.txt AC 74 ms 20820 KB
subtask2_16.txt AC 74 ms 19156 KB
subtask2_17.txt AC 74 ms 18388 KB
subtask2_18.txt AC 74 ms 18388 KB
subtask2_19.txt AC 72 ms 21332 KB
subtask2_20.txt AC 73 ms 21204 KB
subtask2_21.txt AC 72 ms 21332 KB
subtask2_22.txt AC 72 ms 22612 KB