結果

問題 No.2027 (1, 2, 3, …, N) 's Subset Sum
コンテスト
ユーザー Raysphere24
提出日時 2022-08-29 20:25:12
言語 C#
(.NET 10.0.201)
コンパイル:
dotnet_c
実行:
/usr/bin/dotnet_wrap
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 737 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 11,400 ms
コンパイル使用メモリ 177,236 KB
実行使用メモリ 204,884 KB
最終ジャッジ日時 2026-05-06 22:51:19
合計ジャッジ時間 15,559 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (90 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net10.0/main.dll
  main -> /home/judge/data/code/bin/Release/net10.0/publish/

ソースコード

diff #
raw source code

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