結果

問題 No.315 世界のなんとか3.5
ユーザー ぴろずぴろず
提出日時 2015-12-08 13:46:11
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,369 bytes
コンパイル時間 2,063 ms
コンパイル使用メモリ 79,816 KB
実行使用メモリ 75,896 KB
最終ジャッジ日時 2023-10-12 21:38:14
合計ジャッジ時間 33,199 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
55,616 KB
testcase_01 AC 170 ms
56,272 KB
testcase_02 AC 162 ms
56,372 KB
testcase_03 AC 120 ms
55,584 KB
testcase_04 AC 120 ms
55,652 KB
testcase_05 AC 151 ms
55,928 KB
testcase_06 AC 171 ms
55,928 KB
testcase_07 AC 151 ms
56,176 KB
testcase_08 AC 143 ms
56,044 KB
testcase_09 AC 120 ms
55,916 KB
testcase_10 AC 118 ms
55,896 KB
testcase_11 AC 119 ms
56,096 KB
testcase_12 AC 286 ms
61,232 KB
testcase_13 AC 293 ms
61,168 KB
testcase_14 AC 1,162 ms
68,736 KB
testcase_15 AC 1,091 ms
68,576 KB
testcase_16 AC 1,186 ms
68,256 KB
testcase_17 AC 1,178 ms
68,412 KB
testcase_18 AC 328 ms
61,736 KB
testcase_19 AC 341 ms
62,256 KB
testcase_20 AC 347 ms
59,908 KB
testcase_21 AC 344 ms
62,064 KB
testcase_22 AC 1,210 ms
68,368 KB
testcase_23 AC 1,195 ms
68,516 KB
testcase_24 AC 1,087 ms
68,536 KB
testcase_25 AC 1,126 ms
69,108 KB
testcase_26 AC 1,098 ms
69,052 KB
testcase_27 AC 1,174 ms
68,588 KB
testcase_28 AC 1,235 ms
69,088 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 AC 460 ms
64,244 KB
testcase_34 TLE -
testcase_35 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

package no315;

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static long MOD = 1000000007;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String a = sc.next();
		String b = sc.next();
		int zeros = sc.next().length() - 1;
		long bb = aho(b,zeros);
		long aa = aho(new BigInteger(a).subtract(BigInteger.ONE).toString(),zeros);
		System.out.println((bb + MOD - aa) % MOD);
	}
	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