結果

問題 No.315 世界のなんとか3.5
ユーザー ぴろずぴろず
提出日時 2015-12-08 14:22:24
言語 Java19
(openjdk 21)
結果
AC  
実行時間 620 ms / 2,000 ms
コード長 2,708 bytes
コンパイル時間 2,211 ms
コンパイル使用メモリ 74,548 KB
実行使用メモリ 71,348 KB
最終ジャッジ日時 2023-10-12 21:55:08
合計ジャッジ時間 15,860 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,628 KB
testcase_01 AC 152 ms
55,948 KB
testcase_02 AC 169 ms
56,340 KB
testcase_03 AC 122 ms
55,680 KB
testcase_04 AC 122 ms
55,792 KB
testcase_05 AC 152 ms
55,828 KB
testcase_06 AC 169 ms
55,712 KB
testcase_07 AC 152 ms
56,148 KB
testcase_08 AC 155 ms
56,144 KB
testcase_09 AC 120 ms
55,788 KB
testcase_10 AC 121 ms
56,176 KB
testcase_11 AC 121 ms
55,768 KB
testcase_12 AC 293 ms
61,364 KB
testcase_13 AC 293 ms
61,292 KB
testcase_14 AC 425 ms
63,820 KB
testcase_15 AC 398 ms
64,320 KB
testcase_16 AC 429 ms
64,060 KB
testcase_17 AC 413 ms
63,960 KB
testcase_18 AC 336 ms
59,616 KB
testcase_19 AC 319 ms
61,740 KB
testcase_20 AC 346 ms
61,420 KB
testcase_21 AC 340 ms
61,620 KB
testcase_22 AC 435 ms
64,156 KB
testcase_23 AC 444 ms
64,164 KB
testcase_24 AC 413 ms
63,736 KB
testcase_25 AC 422 ms
64,116 KB
testcase_26 AC 423 ms
63,736 KB
testcase_27 AC 423 ms
64,048 KB
testcase_28 AC 435 ms
64,000 KB
testcase_29 AC 536 ms
71,348 KB
testcase_30 AC 509 ms
70,348 KB
testcase_31 AC 529 ms
69,432 KB
testcase_32 AC 549 ms
69,884 KB
testcase_33 AC 412 ms
63,736 KB
testcase_34 AC 576 ms
69,776 KB
testcase_35 AC 620 ms
69,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no315;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

	public static long MOD = 1000000007;
	public static long stime;
	public static void main(String[] args) throws FileNotFoundException {
		Scanner sc = new Scanner(System.in);
		//Scanner sc = new Scanner(new File("in.txt"));
		stime = System.nanoTime();
		String a = sc.next();
		String b = sc.next();
		int zeros = sc.next().length() - 1;
		long bb = aho(b,zeros);
		char[] as = a.toCharArray();
		for(int i=as.length-1;i>=0;i--) {
			if (as[i] >= '1') {
				as[i]--;
				break;
			}
			as[i] = '9';
		}
		long aa = aho(String.valueOf(as),zeros);
		System.out.println((bb + MOD - aa) % MOD);
	}
	public static void lap() {
		System.out.println((System.nanoTime() - stime) / 1000000);
	}
	public static boolean useNaive = false;
	public static long aho(String ss,int zeros) {
		if (useNaive && ss.length() <= 5) {
			return naiveAho(Integer.parseInt(ss), zeros);
		}
		char[] s = ss.toCharArray();
		int n = Math.max(0,s.length - 3 - zeros);
		int[][][] dp = new int[2][4][n+1];
		dp[1][0][0] = 1;
		for(int i=0;i<n;i++) {
			for(int large=0;large<2;large++) {
				for(int j=0;j<4;j++) {
					int max = large == 0 ? 9 : s[i] - '0';
					for(int k=0;k<=max;k++) {
						int nlarge = large & (k == max ? 1 : 0);
						int nj = (j == 3 || k == 3) ? 3 : (j + k) % 3;
						dp[nlarge][nj][i+1] += dp[large][j][i];
						if (dp[nlarge][nj][i+1] >= MOD) {
							dp[nlarge][nj][i+1] -= MOD;
						}
					}
				}
			}
		}
		long ans = 0;
		int max = 1000;
		int step = 8;
		for(int i=0;i<zeros;i++) {
			max *= 10;
			step *= 10;
		}
		int tail = Integer.parseInt(ss.substring(n, ss.length()));
		for(int i=0;i<max;i++) {
			if (i % step == 0) {
				continue;
			}
			int sum = 0;
			boolean three = false;
			int x = i;
			while(x > 0) {
				int d = x % 10;
				sum += d;
				if (d == 3) {
					three = true;
					break;
				}
				x /= 10;
			}

			for(int large=0;large<2;large++) {
				if (large == 1 && i > tail) {
					continue;
				}
				for(int j=0;j<4;j++) {
					if (j == 3 || three || (j+sum)%3==0) {
						if (dp[large][j][n] > 0) {
//							System.out.println(i + "," + large + "," + j + "," + n);
						}
						ans += dp[large][j][n];
					}
				}
			}
		}
		return ans % MOD;

	}
	public static long naiveAho(int max,int zeros) {
		long ans = 0;
		int step = 8;
		for(int i=0;i<zeros;i++) {
			step *= 10;
		}
		for(int i=0;i<=max;i++) {
			if (i % step == 0) {
				continue;
			}
			int x = i;
			boolean three = false;
			while(x > 0) {
				int d = x % 10;
				if (d == 3) {
					three = true;
					break;
				}
				x /= 10;
			}
			if (i % 3 == 0 || three) {
				ans++;
			}
		}
		return ans;
	}
}
0