結果

問題 No.315 世界のなんとか3.5
ユーザー GrenacheGrenache
提出日時 2015-12-09 09:51:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 963 ms / 2,000 ms
コード長 4,969 bytes
コンパイル時間 4,066 ms
コンパイル使用メモリ 79,332 KB
実行使用メモリ 51,892 KB
最終ジャッジ日時 2024-09-14 20:57:02
合計ジャッジ時間 19,792 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
36,692 KB
testcase_01 AC 56 ms
36,944 KB
testcase_02 AC 94 ms
39,316 KB
testcase_03 AC 54 ms
37,020 KB
testcase_04 AC 54 ms
37,164 KB
testcase_05 AC 58 ms
37,012 KB
testcase_06 AC 93 ms
39,544 KB
testcase_07 AC 58 ms
37,000 KB
testcase_08 AC 59 ms
36,724 KB
testcase_09 AC 54 ms
37,032 KB
testcase_10 AC 55 ms
36,724 KB
testcase_11 AC 55 ms
37,020 KB
testcase_12 AC 204 ms
41,280 KB
testcase_13 AC 202 ms
41,200 KB
testcase_14 AC 389 ms
44,092 KB
testcase_15 AC 516 ms
45,952 KB
testcase_16 AC 437 ms
45,120 KB
testcase_17 AC 525 ms
45,952 KB
testcase_18 AC 302 ms
44,560 KB
testcase_19 AC 305 ms
43,036 KB
testcase_20 AC 451 ms
45,420 KB
testcase_21 AC 455 ms
45,252 KB
testcase_22 AC 474 ms
45,484 KB
testcase_23 AC 477 ms
45,476 KB
testcase_24 AC 393 ms
43,940 KB
testcase_25 AC 490 ms
45,592 KB
testcase_26 AC 429 ms
44,184 KB
testcase_27 AC 509 ms
45,172 KB
testcase_28 AC 779 ms
47,688 KB
testcase_29 AC 826 ms
48,756 KB
testcase_30 AC 963 ms
51,892 KB
testcase_31 AC 939 ms
51,808 KB
testcase_32 AC 682 ms
46,656 KB
testcase_33 AC 525 ms
45,464 KB
testcase_34 AC 864 ms
48,972 KB
testcase_35 AC 885 ms
49,444 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_yukicoder315 {

    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 = sc.nextInt();

        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;
	static final int DMAX = 5;

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

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

		int d = 3;
		int[][][][] dp = new int[2][2][d][p];
		int[][][][] dp2 = new int[2][2][d][p];
		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';
			int nowj = (j - 1) % 2;
			int nextj = j % 2;

			for (int l = 0; l < 2; l++) {
				for (int i = 0; i < d; i++) {
					if (j <= DMAX) {
						Arrays.fill(dp[nextj][l][i], 0);
						Arrays.fill(dp2[nextj][l][i], 0);
					} else {
						dp[nextj][l][i][0] = 0;
						dp2[nextj][l][i][0] = 0;
					}
				}
			}

			for (int i = 0; i < d; i++) {
				for (int k = 0; k < 10; k++) {
					int nexti = (i + k) % d;
					for (int l = 0; l < 2; l++) {
						int nextl = l;
						if (k == 3) {
							nextl = 1;
						}

						if (j <= DMAX) {
							for (int i8 = 0; i8 < p; i8++) {
								int nexti8 = (i8 + k * mod8[j - 1]) % p;
								int tmp = dp[nowj][l][i][i8];
								int tmp2 = dp2[nowj][l][i][i8];
								if (tmp == 0 && tmp2 == 0) {
									continue;
								}

								dp[nextj][nextl][nexti][nexti8] += tmp;
								dp[nextj][nextl][nexti][nexti8] %= MOD;
								if (k == num) {
									dp2[nextj][nextl][nexti][nexti8] += tmp2;
									dp2[nextj][nextl][nexti][nexti8] %= MOD;
								} else if (k < num) {
									dp2[nextj][nextl][nexti][nexti8] += tmp;
									dp2[nextj][nextl][nexti][nexti8] %= MOD;
								}
							}

						} else {
							int i8 = 0;
							int nexti8 = 0;
							int tmp = dp[nowj][l][i][i8];
							int tmp2 = dp2[nowj][l][i][i8];
							if (tmp == 0 && tmp2 == 0) {
								continue;
							}

							dp[nextj][nextl][nexti][nexti8] += tmp;
							dp[nextj][nextl][nexti][nexti8] %= MOD;
							if (k == num) {
								dp2[nextj][nextl][nexti][nexti8] += tmp2;
								dp2[nextj][nextl][nexti][nexti8] %= MOD;
							} else if (k < num) {
								dp2[nextj][nextl][nexti][nexti8] += tmp;
								dp2[nextj][nextl][nexti][nexti8] %= MOD;
							}
						}
					}
				}
			}

			if (j == DMAX) {
				for (int l = 0; l < 2; l++) {
					for (int i = 0; i < d; i++) {
						dp[nextj][l][i][0] = 0;
						dp2[nextj][l][i][0] = 0;
						for (int i8 = 1; i8 < p; i8++) {
							dp[nextj][l][i][0] += dp[nextj][l][i][i8];
							dp[nextj][l][i][0] %= MOD;
							dp2[nextj][l][i][0] += dp2[nextj][l][i][i8];
							dp2[nextj][l][i][0] %= MOD;
						}
					}
				}
			}
		}

		int ret = 0;
		for (int l = 0; l < 2; l++) {
			for (int i = 0; i < d; i++) {
				if (n <= DMAX) {
					for (int i8 = 0; i8 < p; i8++) {
						if ((l == 1 || i == 0) && i8 != 0) {
							ret += dp2[n % 2][l][i][i8];
							ret %= MOD;
						}
					}
				} else {
					if ((l == 1 || i == 0)) {
						ret += dp2[n % 2][l][i][0];
						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