結果

問題 No.540 格子点と経路
ユーザー tanzakutanzaku
提出日時 2017-03-28 21:44:05
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,191 bytes
コンパイル時間 3,642 ms
コンパイル使用メモリ 88,208 KB
実行使用メモリ 37,792 KB
最終ジャッジ日時 2024-07-06 13:30:03
合計ジャッジ時間 5,954 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
37,732 KB
testcase_01 AC 50 ms
37,412 KB
testcase_02 AC 49 ms
37,584 KB
testcase_03 AC 49 ms
37,576 KB
testcase_04 AC 40 ms
36,784 KB
testcase_05 AC 49 ms
37,588 KB
testcase_06 WA -
testcase_07 AC 50 ms
37,772 KB
testcase_08 AC 50 ms
37,688 KB
testcase_09 AC 50 ms
37,428 KB
testcase_10 AC 50 ms
37,676 KB
testcase_11 WA -
testcase_12 AC 50 ms
37,700 KB
testcase_13 AC 50 ms
37,716 KB
testcase_14 AC 50 ms
37,792 KB
testcase_15 AC 49 ms
37,296 KB
testcase_16 AC 49 ms
37,572 KB
testcase_17 AC 50 ms
37,756 KB
testcase_18 AC 54 ms
37,572 KB
testcase_19 WA -
testcase_20 AC 49 ms
37,456 KB
testcase_21 WA -
testcase_22 AC 40 ms
36,808 KB
testcase_23 AC 40 ms
36,808 KB
testcase_24 AC 50 ms
37,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.LongStream;


public class _p1509_Validate_naive {
	public static void main(String[] args) throws IOException {
		new _p1509_Validate_naive().solve();
	}
	
	static int mod = (int)1e9+7;
	void solve() throws IOException {
//		if (true) { test(); return; }
		try (final InputValidator in = new InputValidator(System.in)) {
			long w = in.nextInt(0, 100000);
			in.space();
			long h = in.nextInt(0, 100000);
			in.newLine();
			in.eof();
			
			System.out.println(solve(w, h));
		}
	}
	
	void test() {
		dump(2, 2, solve(2, 2), 6);
		dump(5, 3, solve(5, 3), 21);
		dump(1234, 5678, solve(1234, 5678), 7007887);
		dump(0, 0, solve(0, 0), 0);
		dump(5, 5, solve(5, 5), 31);
		dump(5, 3, solve(5, 3), 21);
		dump(5, 4, solve(5, 4), 25);
		dump(4, 4, solve(4, 4), 20);
		dump(6, 6, solve(6, 6), 42);
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				dump(i, j, solve(i, j));
			}
		}
	}
	
	long solve(long w, long h) {
		return Math.max(func(w+1, h+1), func(h+1, w+1));
	}
	
	long func(long w, long h) {
		if (Math.max(w, h) == 1) return 0;
		if (Math.min(w, h) == 1) return 1;
		if (w % 2 == 0 && h % 2 == 0) {
			long[] xs = new long[]{2*(h-1)+1,2*(w-4)+1,2*(h-2)+1,2*(w-6)+1,};
			return func(xs, 2, 0, w, h);
		}
		if (w % 2 == 0 && h % 2 == 1) {
			long[] xs = new long[]{2*(h-1)+1,2*(w-4)+1,2*(h-3)+1,2*(w-4)+1,};
			return func(xs, 0, 1, w, h);
		}
		if (w % 2 == 1 && h % 2 == 1) {
			long[] xs = new long[]{2*(h-1)+1,2*(w-3)+1,2*(h-3)+1,2*(w-5)+1,};
			return func(xs, 0, 1, w, h);
		}
		return 0;
	}
	
	long func(long[] xs, long x, long o, long w, long h) {
		long t = Math.max(0, LongStream.of(xs).map(a->a/8).min().getAsLong());
		long ans = 0;
		for (int i = 0; i < 4; i++) {
			ans += sum(Math.max(0, xs[i]), t);
			xs[i] -= t * 8;
		}
		w -= t * 4;
		h -= t * 4;
//		dump(t, w, h, xs);
		ans += x * t;
		if (Math.max(w, h) <= 1) return ans;
		if (Math.min(w, h) == 1) return ans + 1;
//		dump(xs, t, x, o, ans);
		OUTER: while (true) {
			for (int i = 0; i < 4; i++) {
				ans += Math.max(0, xs[i]);
//				dump(xs, i, t, x, o, ans);
				if (xs[i] <= o) break OUTER;
				xs[i] -= 8;
			}
			ans += x;
		}
//		dump(xs, t, x, o, ans);
		return ans;
	}
	
	long sum(long v, long t) {
		return v * t - 8 * t * (t - 1) / 2;
	}
	
	// for debug
	static void dump(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
	
	static class InputValidator implements Closeable {
		final InputStream in;
		
		final int NO_BUFFER = -2;
		int buffer;
		
		public InputValidator(final InputStream in) {
			this.in = in;
			buffer = NO_BUFFER;
		}
		
		public char nextChar() throws IOException {
			int res = read();
			check("#.".indexOf((char)res) >= 0 || Character.isLetterOrDigit(res));
			return (char)res;
		}

		int read() throws IOException {
			final int res = buffer == NO_BUFFER ? in.read() : buffer;
			buffer = NO_BUFFER;
			return res;
		}
		
		void unread(int ch) throws IOException {
			buffer = ch;
		}
		
		// [low, high]
		long nextLong(long low, long high) throws IOException {
			long val = 0;
			int ch = -1;
			boolean read = false;
			while (true) {
				ch = read();
				if (!Character.isDigit(ch)) break;
				read = true;
				val = val * 10 + ch - '0';
				check(val <= high);
			}
			check(read);
			check(ch >= 0);
			check(val >= low);
			unread(ch);
			return val;
		}
		int nextInt(int low, int high) throws IOException { return (int)nextLong(low, high); }
		int[] nextInts(int n, int low, int high) throws IOException {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt(low, high);
				if (i + 1 != n) space();
			}
			newLineOrEOF();
			return res;
		}
		
		void space() throws IOException { int ch = read(); check(ch == ' '); }
		void newLine() throws IOException {
			int ch = read();
			if (ch == '\r') ch = read();
			check(ch == '\n');
		}
		void newLineOrEOF() throws IOException { int ch = read(); check(ch == '\r' || ch == '\n' || ch < 0); }
		void eof() throws IOException { int ch = read(); check(ch < 0); }
		void check(boolean b) { if (!b) throw new RuntimeException(); }

		@Override
		public void close() throws IOException {
		}
	}
}
0