結果

問題 No.790 ちきんの括弧並べ
ユーザー Grenache
提出日時 2019-04-28 11:10:55
言語 Java
(openjdk 23)
結果
AC  
実行時間 410 ms / 2,000 ms
コード長 1,082 bytes
コンパイル時間 4,509 ms
コンパイル使用メモリ 75,000 KB
実行使用メモリ 52,288 KB
最終ジャッジ日時 2024-12-15 06:52:32
合計ジャッジ時間 6,681 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.Scanner;


public class Main_yukicoder790 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();

		int ans = 0;
		
		// n個のビットのうち、i個のビットが立っているmaskのパターンの生成
		int i = n;
		n = 2 * i;

		int mask = (0x1 << i) - 1;
		while (mask < 0x1 << n) {
			// maskに対する処理

			int cnt = 0;
			for (int j = 0; j < n; j++) {
				if ((0x1 << j & mask) != 0) {
					cnt++;
				} else {
					cnt--;
				}

				if (cnt < 0) {
					break;
				}
			}
			if (cnt == 0) {
				ans++;
			}

			// 次のmaskの計算
			int xx = mask & -mask;
			int yy = mask + xx;
			mask = ((mask & ~yy) / xx >> 1) | yy;
		}
		
		pr.println(ans);
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);
			
		solve();
			
		pr.close();
		sc.close();
	}

	static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0