結果

問題 No.540 格子点と経路
ユーザー tanzakutanzaku
提出日時 2017-04-17 23:23:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,956 bytes
コンパイル時間 4,656 ms
コンパイル使用メモリ 79,112 KB
実行使用メモリ 50,228 KB
最終ジャッジ日時 2024-04-15 08:12:45
合計ジャッジ時間 6,023 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
50,124 KB
testcase_01 AC 58 ms
50,172 KB
testcase_02 AC 57 ms
50,144 KB
testcase_03 AC 58 ms
50,228 KB
testcase_04 AC 60 ms
50,180 KB
testcase_05 AC 58 ms
49,892 KB
testcase_06 AC 58 ms
49,580 KB
testcase_07 AC 60 ms
50,104 KB
testcase_08 AC 58 ms
49,992 KB
testcase_09 AC 60 ms
50,072 KB
testcase_10 AC 58 ms
49,764 KB
testcase_11 AC 57 ms
50,036 KB
testcase_12 AC 58 ms
50,056 KB
testcase_13 AC 58 ms
49,932 KB
testcase_14 AC 57 ms
49,992 KB
testcase_15 AC 58 ms
50,000 KB
testcase_16 AC 56 ms
50,028 KB
testcase_17 AC 60 ms
49,660 KB
testcase_18 AC 60 ms
50,004 KB
testcase_19 AC 62 ms
49,864 KB
testcase_20 AC 59 ms
50,160 KB
testcase_21 AC 59 ms
49,744 KB
testcase_22 AC 58 ms
50,164 KB
testcase_23 AC 60 ms
49,660 KB
testcase_24 AC 61 ms
49,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class _p1509_Validate {
	public static void main(String[] args) throws IOException {
		new _p1509_Validate().solve();
	}
	
	static int mod = (int)1e9+7;
	void solve() throws IOException {
		try (final InputValidator in = new InputValidator(System.in)) {
			int w = in.nextInt(0, 100000);
			in.space();
			int h = in.nextInt(0, 100000);
			in.newLine();
			in.eof();
			
			System.out.println(solve(w, h));
		}
	}
	
	long solve(int w, int h) {
		long ans = 0;
		if (w % 2 == 0 && h % 2 == 0) {
			if (w > h) { final int tmp = w; w = h; h = tmp; }
			while (w >= 1) {
				ans += h * 2 + 1;
				w -= 2;
				{ final int tmp = w; w = h; h = tmp; }
			}
			if (w + h > 0) ans++;
		} else if (w % 2 == 1 && h % 2 == 1) {
			if (w < h) { final int tmp = w; w = h; h = tmp; }
			ans = 1;
			while (h >= 1) {
				ans += 2 * w;
				h -= 2;
			}
		} else {
			if (w % 2 == 1) { final int tmp = w; w = h; h = tmp; }
			ans = 1;
			while (h >= 1) {
				ans += 2 * w;
				h -= 2;
			}
		}
		return ans;
	}
	
	// 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