結果

問題 No.260 世界のなんとか3
ユーザー GrenacheGrenache
提出日時 2015-12-08 23:26:28
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 4,007 bytes
コンパイル時間 3,731 ms
コンパイル使用メモリ 75,460 KB
実行使用メモリ 54,476 KB
最終ジャッジ日時 2023-10-12 22:12:15
合計ジャッジ時間 6,491 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,488 KB
testcase_01 AC 47 ms
49,400 KB
testcase_02 AC 48 ms
49,496 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 47 ms
49,464 KB
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

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_yukicoder260 {

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

        char[] a = sc.next().toCharArray();
        char[] b = sc.next().toCharArray();
        int p = 0;

        int ret = (aho(b, p) - aho(sub(a, '1'), p) + MOD) % MOD;

        pr.println(ret);

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

	static final int MOD = 1_000_000_007;
	static int[] mod8;

	private static int aho(char[] b, int p) {
		int n = b.length;

		if (mod8 == null) {
			mod8 = new int[10_000 + 1];
			long mod = 1;
			for (int i = 0; i <= 10_000; i++) {
				mod8[i] = (int)(mod % MOD);
				mod = (mod * 10) % MOD;
			}
		}

		int d = 3;
		int[][][][] dp = new int[2][d][8][n + 1];
		int[][][][] dp2 = new int[2][d][8][n + 1];
		dp[0][0][0][0] = 1;
		dp2[0][0][0][0] = 1;
		for (int j = 1; j <= n; j++) {
			int num = b[n - j] - '0';
			for (int l = 0; l < 2; l++) {
				for (int i = 0; i < d; i++) {
					for (int i8 = 0; i8 < 8; i8++) {
						for (int k = 0; k < 10; k++) {
							if (k != 3) {
								dp[l][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] += dp[l][i][i8][j - 1];
								dp[l][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] %= MOD;
							} else {
								dp[1][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] += dp[l][i][i8][j - 1];
								dp[1][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] %= MOD;
							}

							if (k == num) {
								if (k != 3) {
									dp2[l][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] += dp2[l][i][i8][j - 1];
									dp2[l][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] %= MOD;
								} else {
									dp2[1][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] += dp2[l][i][i8][j - 1];
									dp2[1][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] %= MOD;
								}
							} else if (k < num) {
								if (k != 3) {
									dp2[l][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] += dp[l][i][i8][j - 1];
									dp2[l][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] %= MOD;
								} else {
									dp2[1][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] += dp[l][i][i8][j - 1];
									dp2[1][(i + k) % d][(i8 + k * mod8[j - 1]) % 8][j] %= MOD;
								}
							}
						}
					}
				}
			}
		}

		int ret = 0;
		for (int l = 0; l < 2; l++) {
			for (int i = 0; i < d; i++) {
				for (int i8 = 0; i8 < 8; i8++) {
					if ((l == 1 || i == 0) && i8 != 0) {
						ret += dp2[l][i][i8][n];
						ret %= MOD;
					}
				}
			}
		}

		return ret;
	}

    private static char[] sub(char[] a, char c) {
    	for (int i = a.length - 1; i >= 0; i--) {
    		if (a[i] - c < 0) {
    			a[i] = (char)(a[i] - c + 10 + '0');
    			c = '1';
    		} else {
    			a[i] = (char)(a[i] - c + '0');
    			break;
    		}
    	}

    	return a;
	}

	@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