結果

問題 No.260 世界のなんとか3
ユーザー uwiuwi
提出日時 2015-08-04 02:20:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 442 ms / 2,000 ms
コード長 4,686 bytes
コンパイル時間 4,266 ms
コンパイル使用メモリ 84,564 KB
実行使用メモリ 59,460 KB
最終ジャッジ日時 2024-04-25 07:48:11
合計ジャッジ時間 14,996 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 247 ms
57,688 KB
testcase_01 AC 238 ms
57,836 KB
testcase_02 AC 232 ms
57,736 KB
testcase_03 AC 442 ms
59,360 KB
testcase_04 AC 436 ms
59,012 KB
testcase_05 AC 285 ms
58,476 KB
testcase_06 AC 285 ms
58,476 KB
testcase_07 AC 382 ms
59,204 KB
testcase_08 AC 356 ms
59,232 KB
testcase_09 AC 297 ms
58,704 KB
testcase_10 AC 388 ms
59,268 KB
testcase_11 AC 386 ms
59,284 KB
testcase_12 AC 343 ms
59,140 KB
testcase_13 AC 278 ms
58,156 KB
testcase_14 AC 382 ms
59,424 KB
testcase_15 AC 282 ms
58,048 KB
testcase_16 AC 355 ms
59,276 KB
testcase_17 AC 375 ms
59,404 KB
testcase_18 AC 322 ms
58,740 KB
testcase_19 AC 363 ms
59,448 KB
testcase_20 AC 369 ms
58,968 KB
testcase_21 AC 325 ms
58,876 KB
testcase_22 AC 380 ms
59,332 KB
testcase_23 AC 275 ms
57,988 KB
testcase_24 AC 358 ms
59,064 KB
testcase_25 AC 368 ms
59,124 KB
testcase_26 AC 362 ms
59,316 KB
testcase_27 AC 241 ms
57,816 KB
testcase_28 AC 411 ms
59,360 KB
testcase_29 AC 424 ms
59,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package q6xx;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Q612XX {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	int mod = 1000000007;
	int[][][][] mem = new int[10002][24][2][2];
	char[] str;
	int pos;
	
	int dfs(int m, int aho, int edge) {
		if(pos == str.length)return ((aho == 1 || m % 3 == 0) && m % 8 != 0) ? 1 : 0;
		if(mem[pos][m][aho][edge] != -1){
			return mem[pos][m][aho][edge];
		}
		long ret = 0;
		pos++;
		for(int i = 0;i <= (edge == 1 ? str[pos-1]-'0' : 9);i++){
			ret += dfs( 
					(m*10+i)%24, 
					aho | (i == 3 ? 1 : 0), 
					edge & (i == str[pos-1]-'0' ? 1 : 0)
					);
		}
		pos--;
		return mem[pos][m][aho][edge] = (int)(ret % mod);
	}
	
	void solve()
	{
		char[] s = ns().toCharArray();
		char[] t = ns().toCharArray();
		long ret = 0;
		fill(mem, -1);
		str = t;
		pos = 0;
		ret += dfs(0, 0, 1);
		fill(mem, -1);
		str = minus1(s);
		ret -= dfs(0, 0, 1);
		if(ret < 0)ret += mod;
		out.println(ret);
	}
	
	public static void fill(Object o, Object v)
	{
		Class<?> comp = o.getClass().getComponentType();
		if(comp != null){
			if(comp.isArray()){
				for(Object c : (Object[])o)fill(c, v);
			}else{
				int len = Array.getLength(o);
				for(int i = 0;i < len;i++)Array.set(o, i, v);
			}
		}
	}
	
	public static char[] minus1(char[] a)
	{
		if(a.length == 1 && a[0] == '0')throw new RuntimeException();
		for(int i = a.length-1;i >= 0;i--){
			if(--a[i] < '0'){
				a[i] = '9';
			}else{
				break;
			}
		}
		if(a[0] == '0'){
			return Arrays.copyOfRange(a, 1, a.length);
		}else{
			return a;
		}
	}

	public void run() throws Exception
	{
//		int n = 100000, m = 99999;
//		Random gen = new Random();
//		StringBuilder sb = new StringBuilder();
//		for (int i = 0; i < n; i++) {
//			sb.append(gen.nextInt(10));
//		}
//		sb.append(" ");
//		for (int i = 0; i < n+1; i++) {
//			sb.append(gen.nextInt(10));
//		}
//		INPUT = sb.toString();
		
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		Thread t = new Thread(null, null, "faa", Runtime.getRuntime().maxMemory()){
			@Override
			public void run() {
				long s = System.currentTimeMillis();
				solve();
				out.flush();
				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
			}
		};
		t.start();
		t.join();
	}
	
	public static void main(String[] args) throws Exception { 
		new Q612XX().run();
	}
	
	private byte[] inbuf = new byte[1024];
	private int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0