結果

問題 No.3135 AAABC
ユーザー ks2m
提出日時 2025-05-02 21:52:43
言語 Java
(openjdk 23)
結果
AC  
実行時間 493 ms / 2,000 ms
コード長 1,061 bytes
コンパイル時間 3,261 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 82,444 KB
最終ジャッジ日時 2025-05-02 21:52:55
合計ジャッジ時間 11,188 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int s = sc.nextInt();
		sc.close();

		List<String> list = new ArrayList<>();
		int end = power(3, n);
		for (int x = 0; x < end; x++) {
			int x2 = x;
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < n; i++) {
				sb.append(x2 % 3);
				x2 /= 3;
			}
			sb.reverse();

			StringBuilder sb2 = new StringBuilder();
			for (int i = 0; i < n; i++) {
				sb2.append((char) ('A' + (sb.charAt(i) - '0')));
			}
			String str = sb2.toString();
			if (str.contains("A") && str.contains("B") && str.contains("C")) {
				list.add(str);
			}
		}
		if (list.size() < s) {
			System.out.println(-1);
		} else {
			System.out.println(list.get(s - 1));
		}
	}

	static int power(int x, int n) {
		if (n == 0) {
			return 1;
		}
		int val = power(x, n / 2);
		val = val * val;
		if (n % 2 == 1) {
			val = val * x;
		}
		return val;
	}
}
0