結果

問題 No.2027 (1, 2, 3, …, N) 's Subset Sum
ユーザー Raysphere24
提出日時 2022-08-29 20:25:12
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 737 bytes
コンパイル時間 18,025 ms
コンパイル使用メモリ 169,584 KB
実行使用メモリ 195,952 KB
最終ジャッジ日時 2024-11-06 18:01:26
合計ジャッジ時間 19,618 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (102 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using static System.Console;

var (N, S) = Util.ReadLongs();

List<long> ans = new();
HashSet<long> visited = new();

bool Search(long n, long s)
{
	if (n == 0) return s == 0;
	if (s < 0) return false;

	if (!visited.Add(N * s + n)) return false;

	if (Search(n - 1, s - n)) {
		ans.Add(n);
		return true;
	}

	return Search(n - 1, s);
}

if (Search(N, S)) {
	WriteLine(ans.Count);
	WriteLine(string.Join(' ', ans));
}
else {
	WriteLine("Impossible");
}

static class Util
{
	public static int[] ReadInts() => ReadLine()!.Split().Select(int.Parse).ToArray();
	public static long[] ReadLongs() => ReadLine()!.Split().Select(long.Parse).ToArray();
	public static void Deconstruct<T>(this T[] v, out T a, out T b) { a = v[0]; b = v[1]; }
}
0